javascript中对Attr(dom中属性)的操作示例讲解

2020-05-22 21:56:41易采站长站整理

            var myAtrr = document.createAttribute(“class”);
            myAtrr.nodeValue = “classStyle”;
            myNode.setAttribute(myAtrr);
            //getAttributeNode 和getAttribute的区别是获取属性值 – getAttribute()
            //getAttribute(“”) 方法返回属性的值。
            //获取属性值 – getAttributeNode()
            //getAttributeNode(“”) 方法返回属性节点,getAttributeNode(”).value取得节点值。


            //对不同的浏览器结果不一样  在这里不做深入研究================================================
            if (myNode.getAttributeNode(“class”) != null)
                alert(“添加成功!!”);
            else
                alert(“没有添加成功”);
            //下面为移除属性的值==========================================================================
            //===========================================================================================
            //            myNode.removeAttribute(“class”);
            //            if (myNode.getAttribute(“class”) == null)
            //                alert(“删除成功!!”);
            //            else
            //                alert(“没有成功”);
            var delNode=myNode.getAttributeNode(“class”);
            if (myNode.getAttribute(“class”) == null)