我需要能够在 JavaScript 中设置所有 html 页面链接以在新选项卡中打开,我该怎么做?
可以归因于div“链接”吗?
这是我的代码:
<!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>
我需要在不使用 HTML 中的 target 属性的情况下执行此操作。我的链接太多了,我也想学习如何做到这一点。
最佳答案
您可以通过执行以下操作将目标附加到“links”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');
});
关于javascript 在新选项卡中打开所有 html 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27109897/