简体   繁体   English

如何将target =“_ blank”添加到指定div中的链接?

[英]How do I add target=“_blank” to a link within a specified div?

Let's say I have the following code: 假设我有以下代码:

<div id="link_other">
    <ul>
        <li><a href="http://www.google.com/">google</a></li>
        <li>
            <div class="some_class">
                dsalkfnm sladkfm
                <a href="http://www.yahoo.com/">yahoo</a>
            </div>
        </li>
    </ul>
</div>

In this case, the JavaScript would add target="_blank" to all links within the div link_other . 在这种情况下,JavaScript会将div target="_blank"到div link_other所有链接。

How can I do that using JavaScript? 我怎么能用JavaScript做到这一点?

/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
// jquery is prettier. :-)

You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect: 您还可以添加标题标签以通知用户您正在执行此操作,以警告它们,因为正如已经指出的那样,它不是用户期望的:

$('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.');

Non-jquery: 非jQuery的:

// Very old browsers
// var linkList = document.getElementById('link_other').getElementsByTagName('a');

// New browsers (IE8+)
var linkList = document.querySelectorAll('#link_other a');

for(var i in linkList){
 linkList[i].setAttribute('target', '_blank');
}

Bear in mind that doing this is considered bad practice in general by web developers and usability experts. 请记住,Web开发人员和可用性专家通常认为这样做是不好的做法。 Jakob Nielson has this to say about removing control of the users browsing experience: Jakob Nielson对此有关删除用户浏览体验的控制权:

Avoid spawning multiple browser windows if at all possible — taking the "Back" button away from users can make their experience so painful that it usually far outweighs whatever benefit you're trying to provide. 如果可能的话,尽量避免产生多个浏览器窗口 - 将“后退”按钮远离用户会使他们的体验变得如此痛苦,以至于它通常远远超过你想要提供的任何好处。 One common theory in favor of spawning the second window is that it keeps users from leaving your site, but ironically it may have just the opposite effect by preventing them from returning when they want to. 有利于产生第二个窗口的一个常见理论是它阻止用户离开你的网站,但具有讽刺意味的是,它可能会产生相反的效果,阻止他们在他们想要的时候返回。

I believe this is the rationale for the target attribute being removed by the W3C from the XHTML 1.1 spec. 我相信这是W3C从XHTML 1.1规范中删除目标属性的基本原理。

If you're dead set on taking this approach, Pim Jager's solution is good. 如果你已经开始采取这种方法,Pim Jager的解决方案是好的。

A nicer, more user friendly idea, would be to append a graphic to all of your external links, indicating to the user that following the link will take them externally. 一个更好,更用户友好的想法是,将图形附加到所有外部链接,向用户指示跟随链接将从外部链接。

You could do this with jquery: 你可以用jquery做到这一点:

$('a[href^="http://"]').each(function() {
    $('<img width="10px" height="10px" src="/images/skin/external.png" alt="External Link" />').appendTo(this)

});

Using jQuery: 使用jQuery:

 $('#link_other a').each(function(){
  $(this).attr('target', '_BLANK');
 });

I use this for every external link: 我将此用于每个外部链接:

window.onload = function(){
  var anchors = document.getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    if (anchors[i].hostname != window.location.hostname) {
        anchors[i].setAttribute('target', '_blank');
    }
  }
}

排队:

$('#link_other').find('a').attr('target','_blank');

用于每个外部链接

$('a[href^="http://"], a[href^="https://"]').attr('target', '_blank');

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

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