jQuery:如何选择具有不等于特定值的属性的所有元素? [英] jQuery: How to select all elements which have an attribute that is not equal to specific value?

查看:883
本文介绍了jQuery:如何选择具有不等于特定值的属性的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在jQuery中选择具有my_attr属性不等于my_value的所有元素?

How could I select in jQuery all elements that have the my_attr attribute which is not equal to my_value ?

如果my_attra并且my_value"4",它应该像这样工作:

If my_attr is a and my_value is "4", it should work like this:

<span>Hello</span>          => Not selected
<span a="5">Stack</span>    => Selected
<span b="4">Overflow</span> => Not selected
<span a="4">!!</span>       => Not selected

推荐答案

要获取不匹配",您可以使用

To get a "doesn't match", you'd use an attribute not-equals selector with (the other part of the question) as has-attribute selector, like this:

$("span[a][a!='4']")

如果您希望它等于,只需取出!以获得属性等于选择器,如下所示:

If you want it to equal, just take out the ! for an attribute-equals selector, like this:

$("span[a][a='5']")

要使用变量,只需将其连接起来,就像这样:

To use a variable, just concatenate, like this:

$("span[" + my_attr + "][" + my_attr + "!='" + my_value + "']")

这篇关于jQuery:如何选择具有不等于特定值的属性的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆