jQuery: Selecting all elements where attribute is

2019-01-04 11:56发布

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']")

3条回答
孤傲高冷的网名
2楼-- · 2019-01-04 12:18

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

.filter(function() {
  return $(this).attr("attrName") > "someValue";
})
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-04 12:20

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

$("selector").filter(function() {
    return parseInt($(this).attr("my-attr")) > 123;
});
查看更多
三岁会撩人
4楼-- · 2019-01-04 12:37

The solution is jQuery.filter():

$("selector").filter(function() {
    return  $(this).attr("my-attr") > 123;
});
查看更多
登录 后发表回答