Home > Web Front-end > JS Tutorial > How does JavaScript get the instance of data attribute in html5

How does JavaScript get the instance of data attribute in html5

黄舟
Release: 2017-07-27 16:29:48
Original
2338 people have browsed it

I used to think that I could only use jquery's data() to get it

Haha, I'm too weak


<!DOCTYPE html>
<html>

<head>
    <title>dataset</title>
    <meta charset="utf-8">
</head>

<body>
    <p id=&#39;p&#39; data-index=&#39;demo&#39;>
    </p>
</body>
<script type="text/javascript">var a = document.getElementById(&#39;p&#39;);
console.log(a)
console.log(a.dataset.index)//demo</script>

</html>
Copy after login

You can directly use dataset to get the data attribute of the DOM element

Okay, you can also use the js method of getting attributes, getAttribute()


a.getAttribute(&#39;data-index&#39;)==>"demo"
Copy after login

The above is the detailed content of How does JavaScript get the instance of data attribute in html5. 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