简体   繁体   English

JQuery选择器问题 - 如何使用target = _blank查找所有HREF?

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

My "JQuery Selector Foo" stinks. 我的“JQuery Selector Foo”很臭。 I need to find all HREF's with a target attr of _blank and replace them with a common window/target. 我需要找到所有HREF与_blank的目标attr并用一个公共窗口/目标替换它们。 Assistance is greatly appreciated! 非常感谢帮助!

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

你的意思是那样的吗?

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 如果您专门查找具有空值的href值,请执行以下操作

$('a[href=""]').each(function() {
  $(a).attr('href', 'theNewUrl');
});

This will catch only anchor tags which have a href attribute that is empty. 这将仅捕获具有空的href属性的锚标记。 It won't work though for anchors lacking an href tag 但是对于缺少href标签的锚点它不起作用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM