jQuery-查找没有目标或特定目标的链接 [英] Jquery - find links that have no target or a certain target

查看:135
本文介绍了jQuery-查找没有目标或特定目标的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到页面上所有没有目标属性或等于' pageContent '的目标属性的链接,对于所有匹配的元素,我想要对他们应用onclick事件.

I'd like to find all the links on a page that either have no target attribute or a target attribute that equals 'pageContent' and for all matched elements I would like to apply an onclick event to them.

onclick事件称为pgTrans('start').我不确定是否重要,但是某些链接可能已经通过jQuery对该代码进行了硬编码或附加了该事件.

The onclick event is called pgTrans('start'). I'm not sure if it matters but some of the links may already have this event hard coded or attached via jQuery.

推荐答案

尝试以下操作:

$('a:not([target]), a[target="pageContent"]').click(function(e) {
    e.preventDefault();
    pgTrans('start');
});

演示: http://jsfiddle.net/UpjmU/

使用> :not() 选择器并在[target]内部,选择所有没有target属性.在第二种情况下,使用[ target ="pageContent" ]选择所有属性target等于pageContent

using :not() selector and inside [target], selects all a that don't have a target attribute. In the second case, using [target="pageContent"], selects all elements that the attribute target equals pageContent

这篇关于jQuery-查找没有目标或特定目标的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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