77

I know that to filter an element with an atttribute called attrName which has value attrValue I do:

filter("[attrName='attrValue']")

but looking at the docs http://api.jquery.com/category/selectors/ I can't see an option to select all elements s.t. attrName>attrValue

Will this work

   filter("[attrName>'attrValue']")
4

4 回答 4

113

You can do this using the function overload of .filter(), like this:

.filter(function() {
  return $(this).attr("attrName") > "someValue";
})
于 2010-04-10T14:14:38.787 回答
29

小心,如果你在玩整数,请确保你使用的是parseInt().

$("selector").filter(function() {
    return parseInt($(this).attr("my-attr")) > 123;
});
于 2017-04-10T19:40:04.467 回答
28

The solution is jQuery.filter():

$("selector").filter(function() {
    return  $(this).attr("my-attr") > 123;
});
于 2010-04-10T14:18:12.587 回答
0

对于 ES6 箭头函数

$("selector").filter((index, el) => {
    return parseInt($(el).attr("attrName")) > 123;
});
于 2021-03-05T06:31:26.010 回答