Js根据class名替换html标签内容 ,在某标签前后添加内容

function getClass(obj, attr) {
        var aArray = [];//定义一个新的空数组  
        var i = 0;
        var aAll = document.getElementsByTagName('*');//获取obj对象下面所有的节点  
        for (i = 0; i < aAll.length; i++) {
            if (aAll[i].className == attr) {//判断当前对象的class名称是不是符合传进来的参数  
                aArray.push(aAll[i]);//如果符合则push到到aArray数组里边  
                alert(aAll[i]);
            }
        }
        return aArray;//最后return一下  
    }


    function insertHtml(where, el, html) {
        where = where.toLowerCase();
        if (el.insertAdjacentHTML) {
            switch (where) {
                case "beforebegin":
                    el.insertAdjacentHTML('BeforeBegin', html);
                    return el.previousSibling;
                case "afterbegin":
                    el.insertAdjacentHTML('AfterBegin', html);
                    return el.firstChild;
                case "beforeend":
                    el.insertAdjacentHTML('BeforeEnd', html);
                    return el.lastChild;
                case "afterend":
                    el.insertAdjacentHTML('AfterEnd', html);
                    return el.nextSibling;
            }
            throw 'Illegal insertion point -> "' + where + '"';
        }
        var range = el.ownerDocument.createRange();
        var frag;
        switch (where) {
            case "beforebegin":
                range.setStartBefore(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el);
                return el.previousSibling;
            case "afterbegin":
                if (el.firstChild) {
                    range.setStartBefore(el.firstChild);
                    frag = range.createContextualFragment(html);
                    el.insertBefore(frag, el.firstChild);
                    return el.firstChild;
                } else {
                    el.innerHTML = html;
                    return el.firstChild;
                }
            case "beforeend":
                if (el.lastChild) {
                    range.setStartAfter(el.lastChild);
                    frag = range.createContextualFragment(html);
                    el.appendChild(frag);
                    return el.lastChild;
                } else {
                    el.innerHTML = html;
                    return el.lastChild;
                }
            case "afterend":
                range.setStartAfter(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el.nextSibling);
                return el.nextSibling;
        }
        throw 'Illegal insertion point -> "' + where + '"';
    }


//调用1
getClass("div","banner")[0].innerHTML='我们来了'; 


//调用2
 //insertHtml('afterBegin', document.getElementById("csContentEnd"), "<dd>在文本前容器内插入内容</dd>");
 //insertHtml('beforeEnd', elem, "<dd>在文本后容器内插入内容</dd>");
 insertHtml('beforeBegin', document.getElementById("csContentEnd"), "<dd>在文本前容器外插入内容</dd>");
 //insertHtml('afterEnd', elem, "<dd>在文本后容器外插入内容</dd>"); 

 

posted @ 2015-08-24 17:43  程序员徐坤  阅读(11208)  评论(0编辑  收藏  举报