[英]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;
}
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.