2013-06-27 49 views
0
<!DOCTYPE HTML> 
<html> 
<body> 
<style type="text/css"> 
#a {background-color:blue;width:100px;height:200px;} 
#b {background-color:red;margin-left:25px;width:50px;height:100px;} 
</style> 
<div id="a">a 
    <div id="b">b</div> 
</div> 
<script> 
document.getElementById("a").onclick = function() {console.log("A is clicked");} 
document.getElementById("b").onclick = function() {console.log("B is clicked");} 
document.onclick = function() {console.log("Document is clicked");} 
</script> 
</body> 
</html> 

问:如何检查js中的事件处理程序的属性?

对于上面的代码,它注册3单击事件处理程序,他们也反对吧?如果是这样,我如何检查这3个处理程序/对象的属性,控制台中的方法?

+0

究竟是什么你试图做/找出?在这种情况下事件处理程序只是功能。它们具有与其他功能相同的属性。 –

回答

1

当你

document.getElementById("a").onclick = function() {console.log("A is clicked");} 

你只是分配给一个函数锚onclick财产,被点击的锚那么当,浏览器就会触发功能。如果你想读这个功能是什么,你只需要输出

console.log(document.getElementById("a").onclick); 
0

我真的不知道你正在尝试做的,也许这可以帮助你:

document.getElementById("a").onclick = function(event) { 
    console.log("A is clicked"); 
    console.log(this); //refers to the source element object 
    console.log(event); //refers to the event object 
} 
相关问题