Examples of manipulation of properties in Attr of dom are explained in javascript

  • 2020-03-30 00:38:42
  • OfStack


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>20120430dom Operation attribute node .htm</title>
    <meta  http-equiv="Content-Type" content="text/html; chareset=utf-8"/>
    <script type="text/javascript">
    //Attr(attribute) is a node but cannot be accessed with firstChild, childNodes, etc
        function testBtn() {
                //  var myNode = document.getElementById("btn");//Get the element tag & NBSP;
                //  var myNodeName = myNode.nodeName;// Get the name of the label above as the button 
                //  var x = myNode.attributes["onclick"].nodeType;//atrributes Is an array of properties    The meaning of this sentence is to find the label as 'btn' the nodeType=2 For attributes 
                // if (x == 2) {
                //    Alert (" you're accessing a property node! ") );
                // }
            //Below is of a certain node properties of modified code = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
            //============================================================================================
            var myNode = document.getElementById("btn");//Get the element tag & NBSP;
            var x = myNode.getAttribute("id");//Gets the id attribute of the tag
            myNode.setAttribute("value", "test");//Modify the id attribute of the tag
            var y = myNode.getAttribute("value");
            alert("id The attribute of " + x + " "Becomes" " + y + " " ");
            //Below is for an element to add attributes = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
            //============================================================================================
            var myAtrr = document.createAttribute("class");
            myAtrr.nodeValue = "classStyle";
            myNode.setAttribute(myAtrr);

            //The difference between getAttributeNode and getAttribute is to get the attribute value -getattribute ()
            //The getAttribute("") method returns the value of the attribute.
            //GetAttributeNode ()
            //The getAttributeNode("") method returns the attribute node, and getAttributeNode("").value gets the node value.
            //Different results for different browsers & NBSP; Here is not to do in-depth study = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
            if (myNode.getAttributeNode("class") != null)
                alert(" Add success!! ");
            else
                alert(" Did not add successfully ");
            //Here to remove the value of the attribute = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
            //===========================================================================================
            //            myNode.removeAttribute("class");
            //            if (myNode.getAttribute("class") == null)
            //                              Alert (" deleted successfully!! ") );
            //            else
            //                              Alert (" failed ");
            var delNode=myNode.getAttributeNode("class");
            if (myNode.getAttribute("class") == null)
                              Alert (" deleted successfully!! ") );
            else
                              Alert (" failed ");
        }
    </script>
</head>
<body>
<h1> Chapter 2 on dom</h1>
<p id="p1">dom Introduction to the </p>
<p> How to use <a href="#" name="link">dom</a></p>
<input id="btn"  type="button" onclick="testBtn()" value=" test "/>
</body>
</html>

Notice the difference between parameter methods with and without Node


Related articles: