JQuery Selector Question -- How to find all HREF's with a target = _blank?
My "JQuery Selector Foo" stinks. I need to find all HREF's with a target attr of _blank and replace them with a commo开发者_如何学Pythonn window/target. Assistance is greatly appreciated!
$("a[target='_blank']").attr('target', 'sometarget');
Do you mean something like that?
try
$("a[target=_blank]").each(function () {
var href = $(this).attr("href"); // retrive href foreach a
$(this).attr("href", "something_you_want"); // replace href attribute with wich u want
// etc
});
let me know what do you want, for more help
If you're specifically looking for href
values which have blank values then do the following
$('a[href=""]').each(function() {
$(a).attr('href', 'theNewUrl');
});
This will catch only anchor tags which have a href attribute that is empty. It won't work though for anchors lacking an href tag
<a href="">Link 1</a> <!-- Works -->
<a>Link 2</a> <!-- Won't work -->
If you need to match the latter then do the following
$('a').each(function() {
var href = $(this).attr('href') || '';
if (href === '') {
$(this).attr('href', 'theNewUrl');
}
});
精彩评论