I'm trying to extract the FIRST data attribute from the following mark-up:
<button class="buttonMiddle" data-vid="1">
<a href="#">who helped them</a>
</button>
<button class="buttonMiddle" data-vid="2">
<a href="#">who helped them</a>
</button>
the JS I've conjured is:
var button1 = $('.buttonMiddle').get(0).data('vid');
console.log(button1);
Chrome's console gives me this:
Uncaught TypeError: Object # has no method 'data'
I've covered the basic troubleshooting --> jQuery is loaded before this js file, they are both loaded just before the closing body tag (so no need for $(function() {// find data-attribute});
.
尝试在此处使用eq()而不是get()
:
var button1 = $('.buttonMiddle').eq(0).data('vid');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.