简体   繁体   中英

Jquery finding elements with HTML5 data attributes, using data object

I'm looking in parent divs for child elements that have a specific HTML5 data attribute. This code was working fine:

$('#parent').find('*[data-something]').css('color', 'red');

Then I found out that you can find HTML data attributes using Jquery's data object http://api.jquery.com/data/#data-html5

So I tried...

$('#parent').find($("#parent").data("something")).css('color', 'red');

...and it doesn't work. Does anyone know why?

JSFIDDLE: http://jsfiddle.net/Qct9v/

Note: I have to use find() because I need to search child elements.

It doesn't work because data() is not a filter method, it is a getter/setter method used to get/set the data value.

If you want to use jQuery data then try something like

$("body").find('*').filter(function(){
    return $(this).data('something') != undefined
})

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