【发布时间】:2023-04-04 21:36:01
【问题描述】:
如何从静态 html 事件处理程序访问 util.log() 方法?
也就是说,当我点击复选框时,我喜欢调用 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.
};
【问题讨论】:
-
您是否希望在单击复选框时发出“嘿”的提示?
-
是的,但最终,如何从硬编码事件中调用对象方法?
标签: javascript events object methods