简体   繁体   中英

Using jquery to find elements with data-attribute that has a specific value?

Is there a basic way via jquery to find all elements that have a data-attribute with a specific value and return those elements as an array?

ie <span data-productID="7">My product</span>

The syntax for selecting elements with a specific attribute value is:

[attr_name = attr_value]

In your specific case, this would be:

$('span[data-product="7"]');

This returns a jQuery collection (not an Array), but it can be accessed much like an array using numerical indices. If you really need an array (perhaps in order to use Array prototype methods), you can use:

$('span[data-product="7"]').makeArray();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM