【发布时间】: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