【问题标题】:Stop New Tab opening when clicking (Ctrl + Click ) on a link without href detail change在链接上单击 (Ctrl + 单击) 时停止打开新选项卡而不更改 href 详细信息
【发布时间】:2015-11-27 06:29:48
【问题描述】:

我想在使用 javascript ( jQuery ) 单击链接时停止打开新标签页。我找到了这段代码

 <html>
<head>
  <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
</head>
<body>

<h3>CTRL+CLICK is disabled </h3>
<a href="https://www.google.co.in">Google</a>
<a href="https://www.yahoo.com">Yahoo</a>

<script>
$(document).ready(function()
{
    $('a').each(function() {
        var href= $(this).attr('href');
        $(this).attr('href','javascript:void(0);');
        $(this).attr('jshref',href);
    });
    $('a').bind('click', function(e) 
    {
        e.stopImmediatePropagation();           
        e.preventDefault();
        e.stopPropagation();
        var href= $(this).attr('jshref');
        if ( !e.metaKey && e.ctrlKey )
            e.metaKey = e.ctrlKey;
        if(!e.metaKey)
        {
            location.href= href;
        }
        return false;
})

});
</script>

</body>
</html>

此代码将 href 详细信息更改为 jshref。但就我而言,我无法更改 href 详细信息。我该怎么办?

【问题讨论】:

  • 你到底想做什么?看起来您的问题令人困惑。
  • @PraveenKumar:抱歉我的表达不好。作为标题,我想在链接上单击(Ctrl + Click)时停止打开新标签。上面的代码可以做到,但出现其他问题,所以我不能使用它。你能告诉我其他方法吗?
  • 还有什么问题?
  • ctrl + click 是浏览器的功能,你不应该阻止它

标签: javascript jquery html clicking


【解决方案1】:

你几乎做到了,改变这个

$('a').bind('click', function(e) 
{
    e.stopImmediatePropagation();           
    e.preventDefault();
    e.stopPropagation();
    var href= $(this).attr('jshref');
    if ( !e.metaKey && e.ctrlKey )
        e.metaKey = e.ctrlKey;
    if(!e.metaKey)
    {
        location.href= href;
    }
    return false;
});

到这里

$('a').bind('click', function(e) 
{
    e.preventDefault();
    var href= $(this).attr('jshref');
    if ( !e.metaKey && e.ctrlKey )
        e.metaKey = e.ctrlKey;
    else
        location.href= href;
}

这里是jsfiddle

编辑:我更新了 jsfiddle,我只是添加了 e.preventDefault() 并删除了 $(this).attr('href','javascript:void(0);')

【讨论】:

  • 在您的情况下,href 详细信息更改为“javascript:void(0);”。你可以不改变上面的href细节吗?谢谢。
  • 当我 Ctrl+单击时,仍然会打开一个新窗口。
  • @DoThanhTung 不适合我,你用什么浏览器?
  • 抱歉,chrome 没问题,但我用的是 IE11。可以用IE11测试吗?谢谢。
  • @DoThanhTung 奇怪的是 IE 绑定它甚至没有触发,我现在帮不了你,我回家试试看
【解决方案2】:

试试这个,当用户鼠标悬停时不会看到 javascript:void()

$(document).ready(function()
{
    $('a').bind('click', function(e) {
    var href= $(this).attr('href');
    $(this).attr('href','javascript:void(0);');
    $(this).attr('jshref',href);
    var href= $(this).attr('jshref');
    if ( !e.metaKey && e.ctrlKey )
        e.metaKey = e.ctrlKey;
    if(!e.metaKey)
    {
        location.href= href;
    }
    $(this).attr('href',href);
    return false;
});
});

【讨论】:

    【解决方案3】:
      $('#a').bind('click', function(e) {      
            e.preventDefault(); // Stop right click on link
            return false;     
        });
        var control = false;
        $(document).on("keyup keydown", function(e) {
            control = e.ctrlKey;
        });
        $("a").on("click", function(e) {
            var href= $(this).attr('href');     
            if (control && href=='javascript:;' || href == 'javascript:void(0);' || href == 'javascript:void();' || href == '#') {
                return false; // Stop ctrl + click on link
            }else {
                window.open(href, '_blank'); 
            } 
        });
    

    【讨论】:

    • 纯代码答案不是有效答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    相关资源
    最近更新 更多