Home >Web Front-end >JS Tutorial >How does JavaScript get the instance of data attribute in html5
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='p' data-index='demo'> </p> </body> <script type="text/javascript">var a = document.getElementById('p'); 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('data-index')==>"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!