javascript - jQuery错误:Uncaught TypeError: element.attr is not a function
PHP中文网
PHP中文网 2017-04-10 17:13:40
0
3
450

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>
PHP中文网
PHP中文网

认证0级讲师

Antworte allen(3)
伊谢尔伦

element 改成 $(element),element不是jq对象所以attr方法不能用

阿神

attr("name") 是jquery对象的方法,原生js的方法是getAttribute()。你的代码可以写成$(element).attr("name")或者element.getAttribute("name")。

刘奇

顺数下来的第8行的element改为$(element)。

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage