我正在尝试向 HTML 选择对象动态添加更改事件。我似乎无法让它工作。我需要调用 dropDownChange 并传递两个参数 this 和 nextDepth 的函数。
var nextDepth = (int)......
var selectBox = ......
selectBox.onchange += function(nextDepth){dropDownChange(this, nextDepth);};
不太确定如何解决这个问题……我认为这可行。非常感谢您的任何帮助。
谢谢!
最佳答案
function changeonchange(){
var selectBox = document.getElementById("selectBox");
var nextDepth = getIntFromSomeWhere();
selectBox.onchange = function() {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
dropDownChange(targ, nextDepth);
};
}
您可以从 http://www.quirksmode.org/js/events_properties.html 获得更多关于事件的信息每次调用“changeonchange”时,都会创建一个新范围来保存局部变量,并且该范围会随新的 onchange 处理程序一起携带。
关于Javascript 动态 OnChange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6318385/