2014-11-24 142 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

相關問題