jQuery错误:Uncaught TypeError: element.attr is not a function
发生错误的语句是if括号里面的那句话。
<script>
//...
highlight: function (element, errorClass) {
$(element).fadeOut(function () {
$(element).fadeIn();
});
$(element).closest(".form-group").addClass(errorClass);
//if条件里面的语句报错:Uncaught TypeError: element.attr is not a function
if(element.attr("name") == "category" || element.attr("name") == "location"){
$(element).next("p.input-group-btn")
.children("button:first")
.css("border-color", "#d9534f");
}
},
//...
</script>
阿神2017-04-10 17:15:40
attr("name") 是jquery对象的方法,原生js的方法是getAttribute()。你的代码可以写成$(element).attr("name")或者element.getAttribute("name")。