开发者

jQuery .find() 2 matching arguments

开发者 https://www.devze.com 2023-03-10 16:53 出处:网络
I have an unordered list with multiple properties per element and I want to find all elements that have both properties.

I have an unordered list with multiple properties per element and I want to find all elements that have both properties.

var results = $('#mylist').find(function() {
   开发者_如何学Go return
        $(this).attr('data-label') == 'red' &&
        $(this).attr('data-size') == 1;
});

I attached an example in the link below:

http://jsfiddle.net/nbz4H/1/


Just use a single selector:

$('li[data-label="red"][data-size="1"]').css('color','red');

Example: http://jsfiddle.net/niklasvh/RyR87/


jQuery's find doesn't take a function as a parameter. That's why this doesn't work.

What you need is to construct an appropriate CSS selector. Something like:

 results = $('#mylist [data-label="red"][data-size="1"]');
0

精彩评论

暂无评论...
验证码 换一张
取 消