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

How does JavaScript get the instance of data attribute in html5

黄舟
黄舟Original
2017-07-27 16:29:482245browse

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>

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"

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!

Statement:
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