右击是Javascript事件吗?如果是,我该如何使用它?
当前回答
您可能想尝试以下属性:
按钮- (caniuse); Which - (caniuse)(弃用)。
function onMouseDown(e)
{
if (e.which === 1 || e.button === 0)
{
console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 2 || e.button === 1)
{
console.log('Middle mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 3 || e.button === 2)
{
console.log('Right mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 4 || e.button === 3)
{
console.log('Backward mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 5 || e.button === 4)
{
console.log('Forward mouse button at ' + e.clientX + 'x' + e.clientY);
}
}
window.addEventListener("mousedown", onMouseDown);
document.addEventListener("contextmenu", function(e)
{
e.preventDefault();
});
相关:演示
操作系统
On Windows and Linux there are modifier keys Alt, Shift and Ctrl. On Mac there’s one more: Cmd, corresponding to the property metaKey... Even if we’d like to force Mac users to Ctrl+click – that’s kind of difficult. The problem is: a left-click with Ctrl is interpreted as a right-click on MacOS, and it generates the contextmenu event, not click like Windows/Linux. So if we want users of all operating systems to feel comfortable, then together with ctrlKey we should check metaKey. For JS-code it means that we should check if (event.ctrlKey || event.metaKey)...
在本章中,我们将详细介绍鼠标事件及其属性……
来源:https://amazon.com/dp/B07DZWLPG9
其他回答
如果您想检测鼠标右键单击,就不应该使用MouseEvent。哪个属性是不标准的,浏览器之间有很大的不兼容性。你应该使用MouseEvent.button。它返回一个表示给定按钮的数字:
0:主按钮按下,通常是左键或未初始化状态 1:辅助按钮按下,通常是车轮按钮或中间按钮(如果有) 2:二级按钮按下,通常是右键 3:第四个按钮,通常是浏览器返回按钮 4:第五个按钮,通常是浏览器前进按钮
MouseEvent。按钮处理的输入类型比标准鼠标更多:
按钮的配置可能与标准不同 “从左到右”的布局。配置为左撇子使用的鼠标可以 将按钮动作反转。有些指向设备只有一个 按钮,并使用键盘或其他输入机制来指示主, 二级、辅助性等。其他的可能有许多映射到的按钮 不同的功能和按钮值。
参考:
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
是的,虽然w3c说右键点击可以通过点击事件检测到,onClick不是通过在通常的浏览器中单击右键触发的。
事实上,右键只触发onMouseDown onMouseUp和onContextMenu。
因此,您可以将“onContextMenu”视为右键单击事件。它是一个HTML5.0标准。
如果你想调用函数,而右击事件意味着我们可以使用以下
<html lang="en" oncontextmenu="func(); return false;">
</html>
<script>
function func(){
alert("Yes");
}
</script>
您可以使用事件窗口。Oncontextmenu,例如:
窗口。Oncontextmenu = function () { alert(右键) } <h1>请右键点击这里!< / h1 >
您可能想尝试以下属性:
按钮- (caniuse); Which - (caniuse)(弃用)。
function onMouseDown(e)
{
if (e.which === 1 || e.button === 0)
{
console.log('Left mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 2 || e.button === 1)
{
console.log('Middle mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 3 || e.button === 2)
{
console.log('Right mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 4 || e.button === 3)
{
console.log('Backward mouse button at ' + e.clientX + 'x' + e.clientY);
}
if (e.which === 5 || e.button === 4)
{
console.log('Forward mouse button at ' + e.clientX + 'x' + e.clientY);
}
}
window.addEventListener("mousedown", onMouseDown);
document.addEventListener("contextmenu", function(e)
{
e.preventDefault();
});
相关:演示
操作系统
On Windows and Linux there are modifier keys Alt, Shift and Ctrl. On Mac there’s one more: Cmd, corresponding to the property metaKey... Even if we’d like to force Mac users to Ctrl+click – that’s kind of difficult. The problem is: a left-click with Ctrl is interpreted as a right-click on MacOS, and it generates the contextmenu event, not click like Windows/Linux. So if we want users of all operating systems to feel comfortable, then together with ctrlKey we should check metaKey. For JS-code it means that we should check if (event.ctrlKey || event.metaKey)...
在本章中,我们将详细介绍鼠标事件及其属性……
来源:https://amazon.com/dp/B07DZWLPG9
推荐文章
- 右击是Javascript事件吗?
- 如何找到JavaScript数组中包含的最大数字?
- 在数组中获取所有选中的复选框
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;