如何从静态html事件处理程序访问util.log()方法?事件处理程序无法访问JavaScript中的Object方法
换句话说,当我点击复选框时,我喜欢调用util.log方法。
onclick =“util.log(\'hey \')”< - 如何在这里重写事件函数?
我知道所有的答案在这里是正确的,但是可以在插入html到innerHTML的时候访问本地方法吗?
谢谢
var utilities = function() {
var util = this;
util.log = function(msg){
alert(msg);
};
var p = document.createElement('p');
p.innerHTML = '<input type="checkbox" onclick="util.log(\'hey\')" value="1">Check me';
// this part has to be plain html, because there is this table generator class
// will insert this to cell as innerHTML. That's why I can't change the table class.
};
你想点击复选框时会显示“嗨”的提示? – 2010-02-05 02:30:35
是的,但最终,如何从硬编码事件调用对象方法? – Nizzy 2010-02-05 02:32:45