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

南笙酒味 提交于 2019-12-07 00:37:31

问题


My "JQuery Selector Foo" stinks. I need to find all HREF's with a target attr of _blank and replace them with a common window/target. Assistance is greatly appreciated!


回答1:


$("a[target='_blank']").attr('target', 'sometarget');

Do you mean something like that?




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/7507009/jquery-selector-question-how-to-find-all-hrefs-with-a-target-blank

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