2010-02-05 22 views

回答

1
// we're in some internal scope here 
var x = 10; 
var fn = function(e) { 
    wrappedFunction(e, x); 
} 

//add 
o.addEventListener('click', fn, false); 

// create remover 
var remover = function() { 
    o.removeEventListener('click', fn, false); 
} 


//save the remover for later or return it - when it's called from whatever scope the event is removed 
remover(); 
+0

不行的跨瀏覽器。您必須使用IE的attachEvent進行分支。 – Marco 2010-02-05 17:28:51

0

當你說「屬性」,你的意思是參數/參數?

如果是這樣,您可以動態分配接受參數的事件處理程序。在下面的例子中,參數testValue傳遞給動態分配的事件處理程序:

<html> 
<head> 
<title>Test</title> 
</head> 
<body> 

<input id="testInput" type="text"/> 

<script type="text/javascript"> 
    var testValue = "Success."; 
    document.getElementById("testInput").onkeydown = function() { 
     test(testValue); } 

    function test(testValue) { 
     alert(testValue); 
    } 
</script>  
</body> 
</html> 

要刪除的事件處理程序,可以將其分配給null

document.getElementById("testInput").onkeydown = null; 
相關問題