html/javascript:隐藏链接目标并在新标签页中打开链接 [英] html/javascript: Hiding link destination and opening link in new tab
问题描述
我正在使用以下javascript(由
I am using the following javascript (provided by m59 in this previous answer) to hide the destination of a link which usually shows up at the bottom of the browser window when hovering over a link:
<div>
<a data-href="http://www.google.com/"> LINK </a>
<script type="text/javascript">
var anchors = document.querySelectorAll('a[data-href]');
for (var i=0; i<anchors.length; ++i) {
var anchor = anchors[i];
var href = anchor.getAttribute('data-href');
anchor.addEventListener('click', function() {
window.location = href;
});
}
</script>
</div>
这工作得很好,但是我也希望在新标签页中打开链接.我必须如何更改脚本才能这样做?
This works perfectly fine, but I would like the link to open in a new tab as well. How do I have to change the script in order to do so?
我尝试使用window.open('href','_blank');
代替无效的window.location = href;
.
I tried using window.open('href','_blank');
instead of window.location = href;
which did not work.
推荐答案
除非我遗漏了一些东西,最明显的事情就是在该锚点上添加一个目标:
Unless im missing something, the most obvious thing to do is add a target to that anchor:
<a data-href="http://www.google.com/" target="blank"> LINK </a>
这篇关于html/javascript:隐藏链接目标并在新标签页中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!