jQuery在选择器中排除具有某些类的元素 [英] jQuery exclude elements with certain class in selector
问题描述
我想在jQuery中为某些锚标记设置点击事件触发器.
我想在新选项卡中打开某些链接,而忽略具有特定类的链接(在您问我无法将类放置在我试图捕获的链接上(因为它们来自CMS之前)).
我要排除类别为 "button"
或 "generic_link"
我尝试过:
$(".content_box a[class!=button]").click(function (e) {
e.preventDefault();
window.open($(this).attr('href'));
});
但这似乎不起作用,还应该如何做一个OR语句以在排除中包含 "generic_link"
?
您可以使用 .not()方法:
$(".content_box a").not(".button")
或者,您也可以使用:not()选择器:
$(".content_box a:not('.button')")
这两种方法之间几乎没有什么区别,除了.not()
更具可读性(尤其是在链接时),而:not()
则非常快.有关差异的更多信息,请参见此堆栈溢出答案.
I want to setup a click event trigger in jQuery for certain anchor tags.
I want to open certain links in a new tab while ignoring ones with a certain class (before you ask I cannot put classes on the links I am trying to catch as they come from a CMS).
I want to exclude links with class "button"
OR "generic_link"
I have tried:
$(".content_box a[class!=button]").click(function (e) {
e.preventDefault();
window.open($(this).attr('href'));
});
But that doesn't seem to work, also how do I do an OR statement to include "generic_link"
in the exclusion?
You can use the .not() method:
$(".content_box a").not(".button")
Alternatively, you can also use the :not() selector:
$(".content_box a:not('.button')")
There is little difference between the two approaches, except .not()
is more readable (especially when chained) and :not()
is very marginally faster. See this Stack Overflow answer for more info on the differences.
这篇关于jQuery在选择器中排除具有某些类的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!