使用JavaScript检测点击的目标? [英] Detectinig the target of the click using JavaScript?
本文介绍了使用JavaScript检测点击的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检测右键单击的对象或ID或用户?我正在使用onContextMenu来触发一个函数,但我不知道如何检测目标。
How do I detect what object or ID or the user right-clicked on? I'm using the onContextMenu to trigger a function but I don't know how to detect the target.
推荐答案
<html>
<head>
<script type="text/javascript">
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
alert(e.target.nodeName); //or e.target.getAttribute('id')
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function(e) {
alert(window.event.srcElement.nodeName); //or e.srcElement and then like above
window.event.returnValue = false;
});
}
</script>
</head>
<body>
<span>Lorem ipsum...</span><br/>
body content
</body>
</html>
PS。我以前见过类似的代码;)
PS. I've seen similar code before ;)
这篇关于使用JavaScript检测点击的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文