sac*_*hin 10 html javascript events
我试图在html页面上绘制单元格,其中每个单元格都是DIV,我需要能够在任何这些单元格上捕获右键单击事件,我该怎么做?
<script>
function fn(event)
{
alert('hello'+event.button);
}
</script>
<div id="cell01"
class=""
onclick="fn(event);"
style="left: 1471px; width: 24px; height: 14px; top: 64px; text-align: center; position: absolute; background-color: rgb(128, 128, 0);">1</div>
Run Code Online (Sandbox Code Playgroud)
小智 40
最简单的方法
<div style="height: 100px; background-color: red;" oncontextmenu="window.alert('test');return false;"></div>
Run Code Online (Sandbox Code Playgroud)
看看这个:Javascript - 事件属性.鼠标右键的值2虽然也注意到它建议使用mousedown或mouseup事件而不是单击.
以下是显示右键单击检测的页面示例:
function doSomething(e) {
var rightclick;
if (!e) var e = window.event;
if (e.which) rightclick = (e.which == 3);
else if (e.button) rightclick = (e.button == 2);
alert('Rightclick: ' + rightclick); // true or false
}
Run Code Online (Sandbox Code Playgroud)