select onclick that contains _blank

2019-08-12 07:07发布

问题:

i select links with a blank target to set a title attribute

jQuery('a[target~="_blank"]').attr('title', 'New window');

that works.

But when i select the onclick attribute that doesn't work

jQuery('a[onclick~="_blank"]').attr('title', 'New window');

any clues?

the link looks like that:

<a onclick="javascript:this.target=&quot;_blank&quot;" title="" href="http://link.com">link</a>

回答1:

Use below code :

jQuery('a[onclick*="_blank"]').attr('title', 'New window');


回答2:

found that contains is * and not ~

jQuery("a[onclick*='_blank']").attr('alt', 'New window');


回答3:

This seems to work for me using the Attribute Contains Selector -

$("a[onclick*='_blank']")

Could you use that instead?

Demo - http://jsfiddle.net/uZaCW/