0 GP
在javascript中傳遞參數或物件到event或setTimeout
作者:怪鳥│2010-10-20 12:43:28│巴幣:0│人氣:657
不論是何種語言或script,傳遞參數到event一直都是比較困難且難以理解的工作,以下是javascript中透過回傳function來解決傳遞參數到event的問題。
傳遞值
var a = document.getElementsByTagName("input");
for (var i=0; i<a.length;i++) {
a[i].onclick = function(idx) {
return function(e) {
alert(idx);
};
}(i);
}
傳遞node
function handle() {
this.abc = "def";
this.setHandler = function(obj) {
obj.onclick = function(that) {
return function() {
alert(that.abc);
};
}(this);
}
}
var a = new handle();
a.setHandler(document.getElementsByTagName('input')[0]);
setTimeout
function handle() {
this.abc = "def";
this.delayMsg = function(m) {
setTimeout(function(a){
return function(e){
alert(a.abc);
};
}(this), m);
};
}
var a = new handle();
a.delayMsg(600);
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=546020
All rights reserved. 版權所有,保留一切權利