简体   繁体   English

单击(Ctrl +单击)链接时停止打开新标签页,而不更改href的详细信息

[英]Stop New Tab opening when clicking (Ctrl + Click ) on a link without href detail change

I want to stop new tab opening when clicking on a link using javascript ( jQuery ). 在使用javascript(jQuery)单击链接时,我想停止打开新标签页。 I found this code 我找到了这段代码

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

This code changed href detail to jshref . 此代码将href详细信息更改为jshref But in my case, i can't change href detail. 但就我而言,我无法更改href详细信息。 How can I do? 我能怎么做?

You almost did it, change this 你差不多做到了,改变这个

$('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;
});

to this 对此

$('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;
}

Here is jsfiddle 这是jsfiddle

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

Try this, when user mouse over won't see javascript:void() 尝试此操作,当用户将鼠标悬停在上方时看不到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;
});
});
  $('#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'); 
        } 
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按住Ctrl键并单击以获取位置时,防止打开新标签页。 - Prevent opening a new tab when ctrl+click for location.href jQuery模拟Ctrl +单击事件时打开新选项卡时单击 - jQuery simulate ctrl + click when opening new tab on click event 当 ctrl + 在 ReactJS 中单击它时,在新选项卡中打开链接? - Open link in new tab when ctrl + click it in ReactJS? 单击链接而不下载时如何在新选项卡中显示pdf? - how to display pdf in new tab when clicking on link without download? 如何在按Ctrl键并单击带有javascript事件的链接时禁用Firefox的“新标签”操作? - How to disable firefox “new tab” action when pressin ctrl and clicking a link with javascript event? 单击我网站上的链接时打开新标签页,保持当前标签页处于活动状态 - Opening new tab when clicking on a link in my website keeping the current tab active 如何通过Ctrl +单击禁用打开新的浏览器选项卡 - How to disable opening new browser tab by Ctrl + click 在Selenium Webdriver中使用Ctrl +单击组合打开新选项卡 - Opening a new tab using Ctrl + click combination in Selenium Webdriver 单击带有滚轮的链接会在新选项卡中打开,但在执行右键单击+打开新选项卡时不会打开 - clicking on a link with scroll wheel opens in a new tab but not when right-click + open new tab is performed 还原普通链接点击行为的用户脚本,在按住Ctrl并单击时可以在新标签页中打开 - Userscript to restore normal link click behavior to open in a new tab when ctrl+clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM