我有如下HTML代码:
<a href="#" onclick="reloadPage()">Text</a>
当用户通过“鼠标左键”点击链接时,应该调用 reloadPage()。
但是当用户在链接上使用“Ctrl + 单击”或“中间按钮”单击时,我想打开一个新窗口而不使用 reloadPage()。
我该怎么做?
最佳答案
可以引用here .这是我的代码
<a href="#" id="test" onclick="reloadPage(event)">Click Me</a>
<script>
$(document).ready(function() {
$(document).keydown(function(event){
if(event.which==17)
cntrlIsPressed = true;
});
$(document).keyup(function(){
cntrlIsPressed = false;
});
});
var cntrlIsPressed = false;
function reloadPage(mouseButton,event)
{
//event.preventDefault();
if( event.which == 2 ) {
//todo something
//window.open($("#test").attr("href"));
alert("middle button");
return false;
}
if(cntrlIsPressed)
{
//window.open($("#test").attr("href"));
// ctrl + click
return false;
}
//todo something
window.location.href = $("#test").attr("href");
return true;
}
</script>
关于javascript - 如何在不调用 onclick 的情况下在新窗口中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34281902/