简体   繁体   English

未将事件侦听器添加到div元素

[英]Event Listener not getting added to the div element

I have following JavaScript code : 我有以下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),  
};

and

        this._incrementBtn = document.createElement('div');  
        this._incrementBtn .className = this._incrementButtonProperties.enabledClass;  
        this.divElt.appendChild(this._incrementBtn);  

&

this._incrementBtn.addEventListener('click', this._incrementButtonProperties.incrementSelectCallback, false);  

Also, 也,

this._prototype._incrementButtonSelectHandler = function(ctrlRef, appData, params)  
{  

     // + & -  
     alert("_incrementButtonSelectHandler called... ");
}  

But event listener is not getting added :-( If I write document.addEventListener('click', this._incrementButtonProperties.incrementSelectCallback, false); - then listener is getting added but I need it only for "this._incrementBtn" div element. 但是没有添加事件侦听器:-(如果我编写document.addEventListener('click',this._incrementButtonProperties.incrementSelectCallback,false);-那么侦听器将被添加,但是我只需要它用于“ this._incrementBtn” div元素。

Any idea whats going wrong? 知道发生了什么事吗?

Note : Please do not suggest jQuery etc. I do not want to use any framework. 注意:请不要建议使用jQuery等。我不想使用任何框架。

Thanks 谢谢
Sneha 斯内哈

Should it really read this._prototype._incrementButtonSelectHandler = ...? 它真的应该阅读this._prototype._incrementButtonSelectHandler = ...吗?

Shouldn't it be this.prototype? 不应该是this.prototype吗? Otherwise I don't see how this._incrementButtonSelectHandler could correspond to this._prototype._incrementButtonSelectHandler. 否则,我看不到this._incrementButtonSelectHandler如何对应于this._prototype._incrementButtonSelectHandler。

Also, you have a trailing comma in this._incrementButtonProperties {...}, which until Ecmascript 5 was actually invalid, even if most browsers will accept it without any complaining. 此外,您在this._incrementButtonProperties {...}中使用逗号结尾,直到Ecmascript 5实际上无效为止,即使大多数浏览器都接受它也没有任何抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM