[英]What is the jQuery alternative to not(':not( selector )')?
What is the jQuery alternative to not(':not( selector )')? 什么是jQuery的替代品 (':not(selector)')?
Basically lets say this: 基本上可以这样说:
var buttons = $('a.buttons');
I am looking for a particular button with the href as '#measurement' and need to add a class to it. 我正在寻找一个特殊的按钮,其中href为'#measurement',需要为其添加一个类。 The only way I know how to do this is with not(':not( selector )'). 我知道如何做到的唯一方法是使用not(':not(selector)')。
buttons.not(':not([href="#measurement"])').addClass('selected');
There has got to be a better way. 必须有一个更好的方法。
.is() // returns boolean
.has() // looks for items inside each element
Any thing out there? 那里有什么东西吗?
The 2 not
s cancel out, and you get 2 not
取消,你得到
$('a.buttons[href="#measurement"]').addClass('selected');
Docs: http://api.jquery.com/category/selectors/attribute-selectors/ 文档: http : //api.jquery.com/category/selectors/attribute-selectors/
EDIT: If you already have a collection, use .filter
编辑:如果您已经有一个集合,请使用.filter
var buttons = $('a.buttons');
buttons.filter('[href="#measurement"]').addClass('selected');
I believe you want filter
: 我相信你想filter
:
$elements.filter(selector)
so if you already have 所以,如果你已经拥有
var $buttons = $('a.buttons');
you can get the right one by 你可以得到合适的人
var $theButtonIWant = $buttons.filter('[href*="#measurement"]');
var button = $('a.buttons[href*="#measurement"]').addClass('selected');
The [ ] block lets you specify an attribute. []块允许您指定属性。 The *= operator in it specifies that the attribute contains the quoted text. 其中的* =运算符指定该属性包含带引号的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.