js报错Cannot read property 'length' of undefined

网上找到的答案:js报错Cannot read property ‘length‘ of undefined时一般是传入的参数是undefined

我原来的代码是

objTmp = document.getElementById("cke_txtKldgAnswer");
            if (objTmp.value.length == 0) {
                inputChkFail(<br><span class="f14 clLRed">提示:请填写标题!</span>);
                return false;
            } else {
                objTmp.value = TrmEgMark(objTmp.value.Trim());
            }

由于txtKldgAnswer被替换成CKEDITOR.replace(‘txtKldgAnswer‘, { height: ‘500px‘, width: ‘99%‘ });

cke_txtKldgAnswer是CKEDITOR运行后生成div的id,在运行之前是没有的,

document.getElementById("cke_txtKldgAnswer")应该是检测不到元素,于是报错。

解决办法:

 if (CKEDITOR.instances.txtKldgAnswer.getData() == "") {
                inputChkFail(<br><span class="f14 clLRed">提示:请填写答案!</span>);
                return false;
            } else {
                objTmp.value = TrmEgMark(objTmp.value.Trim());
            }

 

js报错Cannot read property 'length' of undefined

上一篇:jquery实用小技巧一


下一篇:整理 .NET Standard - Plugins