开发者

jQuery: Selecting all elements where attribute is greater than a value

开发者 https://www.devze.com 2022-12-26 02:50 出处:网络
I know that to filter an element with an atttribute called attrName which has value attrValue I do: filter(\"[attrName=\'attrValue\']\")

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. at开发者_如何学编程trName>attrValue

Will this work

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


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

.filter(function() {
  return $(this).attr("attrName") > "someValue";
})


The solution is jQuery.filter():

$("selector").filter(function() {
    return  $(this).attr("my-attr") > 123;
});


Be careful, if you are playing with integers make sure you are using parseInt().

$("selector").filter(function() {
    return parseInt($(this).attr("my-attr")) > 123;
});


For ES6 arrow function

$("selector").filter((index, el) => {
    return parseInt($(el).attr("attrName")) > 123;
});
0

精彩评论

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