节点属性、样式设置、兼容性、文档碎片

125 阅读1分钟
节点属性
    getAttribute(“属性名”)获得节点属性
    setAttribute(“属性名”,“值”)设置节点属性
    removeAttribute(“属性名”)删除节点属性
    
    
    实例//节点属性
        
        // document.getElementById("p3").onclick=function(){
        //     console.log(this.getAttribute("id"));//获得属性
        //     this.setAttribute("calass","bg");//设置属性
        // }

        document.getElementById("p3").onclick=function(){
            if(this.getAttribute("class")=="bg"){
                this.removeAttribute("class");
            }else{
                this.setAttribute("class","bg")
            }
        }
        
样式设置
    html属性怎么写js就怎么写
    style属性(行内样式,css样式带-去掉,必须符合国际命名)
    style.cssText属性(行内样式,可以一次性添加多个样式)
    className属性(应用类选择器class=类名)
    
实例:   //注意:用style.classText属性时,需要注意如果属性里含有-要用驼峰发命名
        //cssText可以添加多个属性
        document.getElementById("one").style="border:solid 1px #f00"//利用style属性
        document.getElementById("l1").style.fontSize="30px";
        document.getElementById("l1").style.border="solid 1px #000"
        
兼容性
    IE678支持innerText属性,不支持textContent
    火狐不支持innerText属性,支持textContent
    谷歌支持innerText属性和textCintent
    兼容写法:对象名,textContent=对象名.innerText="字符串"
    
innerText属性:用文本替换当前节点所有子节点以及文本节点  
innerHTML属性:替换节点所有子节点及文本节点
outerText属性————火狐不支持
outerHTML属性————替换当前节点

文档碎片(动态加载内容太多时,可以把内容加载到文档碎片里,最后添加到dom结构,减少dom操作)
    createDocumentFragment()创建文档碎片