当我想在 INSTAGRAM的"新标签" 中打开"中间点击" 的链接时 ,它不会离开当前窗口并在现有页面内打开。
在其他网站中,行为是在浏览器的新标签页中打开链接。
我在几个不同的网站上看到了这一点,但它没有任何意义。
我开始通过记录我通过Jquery点击哪个键来实验它。
HTML部分
<a href="#">Clicker</a>
<div id="log">
</div>
JS部分
$('a').mouseup(function (e) {
var button = 'left click';
if (e.which === 2) {
button = 'middle click';
} else if (e.which == 3) {
button = 'right click';
}
$('#log').append('<span>' + button + '(' + e.which + ')</span><br/>');
e.preventDefault();
e.stopPropagation();
});
$(document).click(function (e) {
if (e.which != 3) {
e.preventDefault();
return false;
}
});
答案 0 :(得分:0)
下面的代码有些过分了,但是可以用!
[编辑]不幸的是,似乎event.preventDefault()
在与“ onmousedown \ onauxclick”事件一起使用时没有任何作用!
<a href="https://www.google.com" onmousedown='if(event.button === 1){window.location=event.target.getAttribute("href"); event.preventDefault()}'>Visit google (onmousedown)</a>
<br>
<a href="https://www.google.com" onauxclick='if(event.button === 1){window.location=event.target.getAttribute("href"); event.preventDefault()}'>Visit google (onauxclick)</a>
答案 1 :(得分:0)
以下代码可用于任何<a>
元素:
<!DOCTYPE html>
<a href="https://www.google.com">Visit google</a>
<br><br>
<a href="https://www.w3schools.com">Visit W3Schools</a>
<br><br>
<div> Div Element 1</div> <br> <div> Div Element 2</div>
<br>
<textarea> ta Element 1</textarea><br><textarea> ta Element 2</textarea><br><textarea rows="50"> ta Element 3</textarea>
<script>
addEventListener("click", Mouse_Button);
addEventListener("mousedown", Mouse_Button);
function Mouse_Button(event){ //________________________
if (event.button === 1){ //"1" is for Mouse Middle Button
if (event.target.nodeName === "A"){ //if tag "<a></a>" element
//alert(event.type + " _ " +event.button + " _ " + event.target.nodeName + " _ " + event.target.getAttribute("href"))
window.open(event.target.getAttribute("href"), "_self"); //"_self" open in same tab
event.preventDefault();
//in Chrome and Firefox, Mouse Middle Button is blocked, so links are not open in new tabs (as expected)
//in Opera and IE9, Mouse Middle Button is not blocked, so links are open in new tabs (Maybe a bug?)
}
}
}
</script>