admin管理员组文章数量:1134247
Let's say I have the following code:
<div id="link_other">
<ul>
<li><a href="/">google</a></li>
<li>
<div class="some_class">
dsalkfnm sladkfm
<a href="/">yahoo</a>
</div>
</li>
</ul>
</div>
In this case, the JavaScript would add target="_blank"
to all links within the div link_other
.
How can I do that using JavaScript?
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
.
How can I do that using JavaScript?
Share Improve this question edited May 12, 2016 at 13:52 ak_ 2,8153 gold badges16 silver badges28 bronze badges asked Apr 29, 2009 at 21:02 kakkalokakkalo 8253 gold badges8 silver badges11 bronze badges 1- 1 Why not let the JavaScript detect which links are external? – James Commented Apr 29, 2009 at 22:45
7 Answers
Reset to default 154/* 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:
// 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. Jakob Nielson has this to say about removing control of the users browsing experience:
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.
If you're dead set on taking this approach, Pim Jager's solution is good.
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:
$('a[href^="http://"]').each(function() {
$('<img width="10px" height="10px" src="/images/skin/external.png" alt="External Link" />').appendTo(this)
});
Using 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');
}
}
}
Inline:
$('#link_other').find('a').attr('target','_blank');
Use this for every external link
$('a[href^="http://"], a[href^="https://"]').attr('target', '_blank');
本文标签: javascriptHow do I add targetquotblankquot to a link within a specified divStack Overflow
版权声明:本文标题:javascript - How do I add target="_blank" to a link within a specified div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736847697a1955370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论