Detectinig the target of the click using JavaScript?
问题 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. 回答1: <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