I'm trying to find a way of disabling the default action of the mouse wheel button which is to open the link in a new tab.
Is that possible?
I'm trying to find a way of disabling the default action of the mouse wheel button which is to open the link in a new tab.
Is that possible?
绑定一个专门检查中间点击的通用点击事件处理程序。在该事件处理程序中,调用e.preventDefault()
:
$("#foo").on('click', function(e) {
if( e.which == 2 ) {
e.preventDefault();
}
});
请注意,并非所有浏览器都支持阻止此默认操作。对我来说,它只适用于 Chrome。Firefox、Opera 和 IE9 都不会通过鼠标中键单击引发 click 事件。他们确实提出了 mouseup 和 mousedown。
这对我有用...
$(document).on("mousedown", "selector", function (ev) {
if (ev.which == 2) {
ev.preventDefault();
alert("middle button");
return false;
}
});
使用 JAVASCRIPT 禁用鼠标滚轮事件:
在IE中:
document.attachEvent('onmousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
在Safari中:
document.addEventListener('mousewheel', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);
在歌剧中:
document.attachEvent('mousewheel', function(e){
if (!e) var e = window.event;
e.returnValue = false;
e.cancelBubble = true;
return false;
}, false);
在火狐中:
document.addEventListener('DOMMouseScroll', function(e){
e.stopPropagation();
e.preventDefault();
e.cancelBubble = false;
return false;
}, false);
我的代码:
$(document).on('auxclick', 'a', function(e) {
if (e.which === 2) { //middle Click
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
return false;
}
return true;