html/javascript:隐藏链接目标并在新标签页中打开链接 [英] html/javascript: Hiding link destination and opening link in new tab

查看:80
本文介绍了html/javascript:隐藏链接目标并在新标签页中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下javascript(由 m59 提供) /questions/21373806/html-remove-destination-from-link>此上一个答案)以隐藏链接的目的地,该链接的目的地通常在悬停在链接上时显示在浏览器窗口的底部:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆