custom-data-attribute

Selecting element by data attribute

这一生的挚爱 提交于 2019-11-25 22:58:09
问题 Is there an easy and straight-forward method to select elements based on their data attribute? For example, select all anchors that has data attribute named customerID which has value of 22 . I am kind of hesitant to use rel or other attributes to store such information, but I find it much harder to select an element based on what data is stored in it. 回答1: $('*[data-customerID="22"]'); You should be able to omit the * , but if I recall correctly, depending on which jQuery version you’re

jQuery how to find an element based on a data-attribute value?

扶醉桌前 提交于 2019-11-25 22:26:50
问题 I\'ve got the following scenario: var el = \'li\'; and there are 5 <li> \'s on the page each with a data-slide=number attribute (number being 1,2,3,4,5 respectively) . I now need to find the currently active slide number which is mapped to var current = $(\'ul\').data(current); and is updated on each slide change. So far my tries have been unsuccessful, trying to construct the selector that would match the current slide: $(\'ul\').find(el+[data-slide=+current+]); does not match/return

jQuery Data vs Attr?

痴心易碎 提交于 2019-11-25 22:15:32
问题 What is the difference in usage between $.data and $.attr when using data-someAttribute ? My understanding is that $.data is stored within jQuery\'s $.cache , not the DOM. Therefore, if I want to use $.cache for data storage, I should use $.data . If I want to add HTML5 data-attributes, I should use $.attr(\"data-attribute\", \"myCoolValue\") . 回答1: If you are passing data to a DOM element from the server, you should set the data on the element: <a id="foo" data-foo="bar" href="#">foo!</a>