Home >Web Front-end >Front-end Q&A >What does the javascript attributes attribute do?
In JavaScript, the attributes attribute can obtain and return the attribute collection of the specified element node, that is, the NamedNodeMap object; the syntax is "element node.attributes".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The attributes attribute returns the attribute collection of the specified node, that is, the NamedNodeMap object.
Tip: You can use the length property to determine the number of properties, and then you can iterate through all property nodes and extract the information you need.
Syntax: node.attributes
Return value: NamedNodeMap object, representing a collection of attributes.
<!DOCTYPE html> <html> <body> <p>点击按钮来查看 button 元素拥有多少属性。</p> <button id="myBtn" onclick="myFunction()">试一下</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myBtn").attributes.length; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
Let’s guess what the result is? The result should be 2 (the id and onclick attributes of the button element). Take a look at the renderings:
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What does the javascript attributes attribute do?. For more information, please follow other related articles on the PHP Chinese website!