toStringcannot read property 'toString()' of undefined
在将其用作时给出错误
document.getElementById('pMonth').addEventListener('click',function(){ calen(this.year.toString()+'-'+(this.currentMonth1-1).toString());},false);
但是代码在用作时完美运行
var tr=this.year.toString()+'-'+(this.currentMonth1-1).toString();
document.getElementById('pMonth').addEventListener('click',function(){ calen(tr);},false);
this.year
并且this.CurrentMonth1
是数值!请告诉我我要去哪里错了!
您需要将上下文绑定到事件处理程序,因为this
这将引用从中返回的DOM元素document.getElementById('pMonth')
因此,您需要:
document.getElementById('pMonth').addEventListener(
'click',
function(){
calen(this.year.toString()+'-'+(this.currentMonth1-1).toString());
}.bind(this),
false
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句