我有以下 JavaScript 代码:
this._incrementButtonProperties = {
"enabled": this.properties.incrementButtonConfig.enabled,
"enabledClass" : "SchedMainCtrlIncrementBtn_En",
"disabledClass" : "SchedMainCtrlIncrementBtn_Ds",
"appData" : this.properties.incrementButtonConfig.appData,
"text" : this.properties.incrementButtonConfig.text,
"incrementSelectCallback" : this._incrementButtonSelectHandler.bind(this),
};
和
this._incrementBtn = document.createElement('div');
this._incrementBtn .className = this._incrementButtonProperties.enabledClass;
this.divElt.appendChild(this._incrementBtn);
&
this._incrementBtn.addEventListener('click', this._incrementButtonProperties.incrementSelectCallback, false);
另外,
this._prototype._incrementButtonSelectHandler = function(ctrlRef, appData, params)
{
// + & -
alert("_incrementButtonSelectHandler called... ");
}
但是事件监听器没有被添加:-(如果我写 document.addEventListener('click', this._incrementButtonProperties.incrementSelectCallback, false); - 那么监听器被添加,但我只需要它用于“this._incrementBtn” div 元素。
知道出了什么问题吗?
注意:请不要建议使用 jQuery 等。我不想使用任何框架。
谢谢
斯内哈
最佳答案
它真的应该读 this._prototype._incrementButtonSelectHandler = ... 吗?
不应该是this.prototype吗?否则我看不到 this._incrementButtonSelectHandler 如何对应于 this._prototype._incrementButtonSelectHandler。
此外, this._incrementButtonProperties {...} 中有一个尾随逗号,直到 Ecmascript 5 实际上是无效的,即使大多数浏览器会毫无怨言地接受它。
关于javascript - 事件监听器未添加到 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14682914/