div
我使用onclick
函数在其他页面上重定向。
但是当我使用ctrl键和鼠标时,链接在下一个标签页和同一个标签页中打开。理想情况下,它应该只在新标签中打开。
代码如下:
<div onclick="window.location='some page url'">
答案 0 :(得分:1)
这样做:
<a href="some page url">
<div></div>
</a>
因此,当您点击它在同一个标签页中打开时,按ctrl键时,它会在新标签页中打开。
编辑:现在试试这个:
<div onclick="doRedirect()"></div>
<script>
$(document).keydown(function(event){
if(event.which=="17")
cntrlIsPressed = true;
});
$(document).keyup(function(){
cntrlIsPressed = false;
});
var cntrlIsPressed = false;
function doRedirect()
{
if(cntrlIsPressed)
{
window.open('some url','_blank');
}
else{
window.location('some url');
}
}
</script>
您还可以在参数中指定网址。
答案 1 :(得分:0)
<div onclick="window.open(
'https://support.wwf.org.uk/earth_hour/index.php?type=individual',
'_blank' // <- This is what makes it open in a new window.
);">
Click to open in tab
</div>
这是工作example