javascript - 事件监听器未添加到 div 元素

标签 javascript addeventlistener

我有以下 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/

相关文章:

javascript - 在 <!-- --> 中包装 javascript

javascript - Bootstrap子菜单点击后隐藏

javascript - 在javascript中过滤数组的有效方法

javascript - 隐藏/显示第 n 项后的列表

javascript - addEventListener 使用 for 循环并传递值

javascript - 在具有各种监听器的 for 循环中使用 addEventListener()

javascript - 提交按钮的Onclick元素未调用JS函数

javascript - getElementsByClassName[i] 返回未定义

javascript - 将事件处理程序添加到构造函数方法

Javascript - 如何使用点击事件和类删除 DOM 元素?