2014-11-24 144 views
-1

我需要能够设置,在JavaScript中,所有的HTML页面链接到新标签中打开,我该怎么办呢?JavaScript打开一个新标签的所有HTML链接?

是否可以归结到div“链接”?

这里是我的代码:如果没有在HTML中使用target属性

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Negócios</title> 


<script type="text/javascript"> 
</script> 



</head> 

<body> 


<div id="links"> 
<p><a href="https://www.facebook.com/groups/ivendi.maringa2/" data-hovercard="/ajax/hovercard/group.php?id=472352436193468" aria-owns="js_1j" aria-haspopup="true" id="js_1k">MARINGÁ - NEGÓCIOS</a></p> 
<p><a href="https://www.facebook.com/groups/638497612850047/" data-hovercard="/ajax/hovercard/group.php?id=638497612850047" aria-owns="js_1l" aria-haspopup="true" id="js_1m">MONTES CLAROS - NEGÓCIOS</a><br> 
</p> 
<p><a href="https://www.facebook.com/groups/aggape/" data-hovercard="/ajax/hovercard/group.php?id=538358279511007" aria-owns="js_1r" aria-haspopup="true" id="js_1s">Sucesso Empresarial</a></p> 
</div> 
</body> 
</html> 

我需要做的。我有太多的链接,我也想学习如何做到这一点。

+0

通过使用目标属性? – epascarello 2014-11-24 16:52:13

+0

只给一个元素一个'target =“_ blank”' – twain 2014-11-24 16:52:28

+0

为什么你需要用Javascript做到这一点?为什么不将target属性设置为_blank? 'My Line' – Robbert 2014-11-24 16:52:33

回答

1

您可以将目标到所有链接的 “链接” 做这个div内:

的Javascript:

window.onload = function(){ 
     var a = document.getElementById('links').getElementsByTagName('a'); 
     for (var i=0; i<a.length; i++){ 
      a[i].setAttribute('target', '_blank'); 
     } 
    } 

jQuery的:

$(document).ready(function(){ 
     $('#links a').attr('target', '_blank'); 
    }); 
+0

在那里,工作!这正是我所说的。 – 023023 2014-11-24 17:39:11

+0

不客气! – baao 2014-11-24 17:39:48

相关问题