简体   繁体   English

如何使用javascript仅将target =“ _ blank”添加到某些链接?

[英]How to use javascript to add target=“_blank” to some links only?

是否可以使用javascript将target="_blank"添加到URL中包含以下内容的所有链接: profile.php?id=

The following should work: 以下应该工作:

var aElems = document.links;

for (var i=0,len = aElems.length; i<len; i++){
    if (aElems[i].href.indexOf('profile.php?id=') != -1){
        aElems[i].target = '_blank';
    }
}

JS Fiddle demo . JS小提琴演示


Edited to improve the performance of the above code, removing document.links and replacing it with document.getElementsByTagName('a') : 编辑以提高上述代码的性能,删除document.links并将其替换为document.getElementsByTagName('a')

var aElems = document.getElementsByTagName('a');

for (var i=0,len = aElems.length; i<len; i++){
    if (aElems[i].href.indexOf('profile.php?id=') != -1){
        aElems[i].target = '_blank';
    }
}

JS Fiddle demo . JS小提琴演示


It's worth noting, though, that using Chris Fulstow 's querySelectorAll() -enabled $('a[href*="profile.php?id="]') approach is marginally faster (in Chromium 14/Ubuntu 11.04): JS Perf speed-test . 不过,值得注意的是,使用Chris Fulstow的启用$('a[href*="profile.php?id="]') querySelectorAll() $('a[href*="profile.php?id="]')方法的速度略快(在Chromium 14 / Ubuntu 11.04中): JS性能测试 Therefore, the following is the fastest (in browsers that support querySelectorAll() ): 因此,以下是最快的(在支持querySelectorAll()浏览器中):

var aElems = document.querySelectorAll('a[href*="profile.php?id="]');

for (var i=0,len = aElems.length; i<len; i++){
    if (aElems[i].href.indexOf('profile.php?id=') != -1){
        aElems[i].target = '_blank';
    }
}

JS Fiddle demo . JS小提琴演示

Above assertion 'fastest' supported by JS Perf comparison , at least in Chromium 14/Ubuntu 11.04. 至少在Chromium 14 / Ubuntu 11.04中,上述JS Perf比较支持“最快”的断言。 Bear in mind, of course, that IE < 8, and Firefox 3 , aren't going to play well with this approach. 当然,请记住IE <8和Firefox 3不能很好地使用这种方法。

Of course, the above should be corrected to: 当然,以上内容应更正为:

var aElems = document.querySelectorAll('a[href*="profile.php?id="]');

for (var i=0,len = aElems.length; i<len; i++){
    aElems[i].target = '_blank';
}

JS Fiddle demo . JS小提琴演示

This is because the if condition was already evaluated by querySelectorAll() , which makes the if entirely redundant. 这是因为if条件已经由querySelectorAll()求值,这使得if完全多余。

Modified JS Perf comparison: http://jsperf.com/anchor-selector-test/3 . 修改后的JS Perf比较:http://jsperf.com/anchor-selector-test/3

另一个jQuery解决方案,只是为了好玩。

$("a[href*='profile.php?id=']").attr("target", "_blank");

Use JQuery or look at their source and port their logic into your own code base: 使用JQuery或查看其源代码并将其逻辑移植到您自己的代码库中:

$(function(){
    $('a').each(function(){
        var $this = $(this);
        if ($this.attr('href').indexOf('profile.php?id=') !== -1) {
            $this.attr('target','_blank');
        }
    });
});

Demo: http://jsfiddle.net/KJv7D/ 演示: http//jsfiddle.net/KJv7D/

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

相关问题 如何仅自动将 target=&quot;_blank&quot; 添加到外部链接? - How to automatically add target=“_blank” to external links only? 将target =“blank”添加到使用javascript的链接 - Adding target=“blank” to links with javascript WordPress主题在链接上阻止target =“_ blank”。 如何为某些外部链接重新激活它? - Wordpress theme blocks target=“_blank” on links. How do I reactivate it for some external links? 有没有办法只为重定向的链接制作“target=&#39;_blank&#39;”? - Is there a way to make "target='_blank'" only for the redirected links? 有条件地将target =“_ blank”添加到与Angular JS的链接中 - Conditionally add target=“_blank” to links with Angular JS 如果链接有 http 或 https,则将目标空白添加到链接 - Add target blank to links if it has http or https 添加 target=&quot;_blank&quot; 以与 JavaScript 链接 - Add target=“_blank” to link with JavaScript 将target =“_ blank”添加到所有链接但内部站点链接 - Add target=“_blank” to all links BUT internal site links 找到所有href值以“http..”开头的链接,然后设置target=”_blank“,使用javascript而不是jquery - find all links that href value begin with "http..", then set target=”_blank“ fo it, use javascript not jquery 如何在JavaScript中使用替换运算符添加target =“ _ blank”? - How to add target=“_blank” using replace operator in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM