开发者

JQuery Selector Question -- How to find all HREF's with a target = _blank?

开发者 https://www.devze.com 2023-04-07 01:55 出处:网络
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

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');
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消