这一行:
this.textActive = this.focused || (this.element.value.length && this.element.value !== this.origValue);
产生此错误消息:Uncaught TypeError:无法读取null的属性'length'
我不确定是什么导致它,但它造成了问题。
refreshClasses: function() {
this.textActive = this.focused || (this.element.value.length && this.element.value !== this.origValue);
this.setStateClass(this.element, this.options.inputFocusClass,this.focused);
this.setStateClass(this.elementParent, this.options.parentFocusClass,this.focused);
this.setStateClass(this.labelFor, this.options.labelFocusClass,this.focused);
this.setStateClass(this.element, this.options.inputActiveClass, this.textActive);
this.setStateClass(this.elementParent, this.options.parentActiveClass, this.textActive);
this.setStateClass(this.labelFor, this.options.labelActiveClass, this.textActive);
},
setStateClass: function(el,cls,state) {
if(!el) return; else if(state) addClass(el,cls); else removeClass(el,cls);
}
}
答案 0 :(得分:0)
在您的代码中,您认为value
属性尚未定义,而不是检查长度,请使用typeof
检查类型:
this.textActive = this.focused || (typeof this.element.value == 'undefined' && this.element.value !== this.origValue);
答案 1 :(得分:0)
我也抓住了这个。 @Nabil是对的。如果值未定义,则它自然不会有长度。
this.textActive = this.focused || this.element.value &&
this.element.value !== this.origValue;
这不适合你吗?