Is there a way to register a global onclick listener that will fire anytime an element is clicked? Need to also get the id of that element.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
document.addEventListener("click", function(evnt){ console.log(evnt.target.id); });
回答2:
You can get id this way using javascript:
window.onclick = function(event) {alert(event.target.id);}
<div id="dID">div</div> <button id="bId">Button</button> <input type="text" id="txtId" class="txtclass" />
回答3:
here is a fiddle
var everything = document.querySelectorAll("body *"); //select every element in the body Array.prototype.forEach.call(everything, function(el){ el.onclick = function(){ var id = this.id; console.log(id); } });