问题
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