2013-06-27 48 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 
} 
相關問題