jQuery:选择属性大于值的所有元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2613648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:53:18  来源:igfitidea点击:

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

jquerycss-selectorsinequalities

提问by Ankur

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

我知道要过滤具有名为 attrName 的属性的元素,该属性具有 attrValue 值,我会这样做:

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

但查看文档http://api.jquery.com/category/selectors/我看不到选择所有元素的选项 st attrName>attrValue

Will this work

这会奏效吗

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

回答by Nick Craver

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

您可以使用 的函数重载来执行此操作.filter(),如下所示:

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

回答by Crozin

The solution is jQuery.filter():

解决方案是jQuery.filter()

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

回答by Julien Le Coupanec

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

请注意,如果您正在玩整数,请确保您使用的是parseInt().

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