javascriptでAttr(domのプロパティ)の操作例を説明します.

3436 ワード

 
  



    20120430dom .htm
   
    <br>    //Attr( ) firstChild childNodes <br>        function testBtn() { <br>                //  var myNode = document.getElementById("btn");// element   <br>                //  var myNodeName = myNode.nodeName;// <br>                //  var x = myNode.attributes["onclick"].nodeType;//atrributes   'btn' nodeType=2 <br>                // if (x == 2) { <br>                //   alert(" !"); <br>                // } <br>            // ================================================================ <br>            //============================================================================================ <br>            var myNode = document.getElementById("btn");// element   <br>            var x = myNode.getAttribute("id");// id <br>            myNode.setAttribute("value", "test");// id <br>            var y = myNode.getAttribute("value"); <br>            alert("id “" + x + "” “" + y + "”"); <br>            // ===================================================================== <br>            //============================================================================================ <br>            var myAtrr = document.createAttribute("class"); <br>            myAtrr.nodeValue = "classStyle"; <br>            myNode.setAttribute(myAtrr); <br> <br>            //getAttributeNode getAttribute - getAttribute() <br>            //getAttribute("") 。 <br>            // - getAttributeNode() <br>            //getAttributeNode("") ,getAttributeNode('').value 。 <p></p> <p>            //   ================================================<br>            if (myNode.getAttributeNode("class") != null)<br>                alert(" !!");<br>            else<br>                alert(" ");<br>            // ==========================================================================<br>            //===========================================================================================<br>            //            myNode.removeAttribute("class");<br>            //            if (myNode.getAttribute("class") == null)<br>            //                alert(" !!");<br>            //            else<br>            //                alert(" ");<br>            var delNode=myNode.getAttributeNode("class");<br>            if (myNode.getAttribute("class") == null)<br>                alert(" !!");<br>            else<br>                alert(" ");<br>        }<br>   


dom


dom


dom





注意裏にNodeとNodeがないパラメータ方法の違いがあります.