What does the javascript attributes attribute do?

青灯夜游
Release: 2021-10-25 17:41:53
Original
2070 people have browsed it

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".

What does the javascript attributes attribute do?

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>
Copy after login

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:

What does the javascript attributes attribute do?

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!