Jquery - find links that have no target or a certain target

女生的网名这么多〃 提交于 2019-12-24 05:39:49

问题


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.

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.


回答1:


Try the following:

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

Demo: http://jsfiddle.net/UpjmU/

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




回答2:


The function will be called twice by any links that already have an event handler to do this attached.

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


来源:https://stackoverflow.com/questions/6797776/jquery-find-links-that-have-no-target-or-a-certain-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!