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).

我想排除与100101类的链接

我试过:

$(".content_box a[class!=button]").click(function (e) {
    e.preventDefault();     
    window.open($(this).attr('href'));
});

但这似乎行不通,还有,我怎么做OR语句才能将100包含在排除项中呢?

推荐答案

您可以使用.not()方法:

$(".content_box a").not(".button")

Alternatively, you can also use the :not() selector:

$(".content_box a:not('.button')")

这两种方法之间几乎没有区别,只是.not()更具可读性(尤其是在链接时),而:not()则稍微快一些.有关差异的更多信息,请参见this Stack Overflow answer.

Jquery相关问答推荐

如何在jQuery中移动表格行?

缺少 .map 资源?

jQuery:使用变量作为 Select 器

寻找 jQuery 事件类型的完整列表

jquery 淡入淡出元素不显示样式为可见性:隐藏的元素

jQuery:获取所选单选按钮的父 tr

可以在不使用for=id的情况下将标签与复选框相关联吗?

确定 JavaScript 值是否为整数?

如何使用 jquery 更改 onclick 事件?

未捕获的 TypeError:data.push 不是函数

Jquery:如何判断元素是否具有某些 css 类/样式

Underscore.js 和 jQuery 互补吗?

如何从 jQuery UI datepicker 获取日期

用作 Google Chrome 书签

.append 之后的 jQuery 函数

如何使用 javascript 展开和折叠

你如何记录jQuery中一个元素触发的所有事件?

如何在文件 Select 上触发事件

jQuery .each() 索引?

如何使用 jQuery Select 第一个父 DIV?