search

Home  >  Q&A  >  body text

javascript - jQuery错误:Uncaught TypeError: element.attr is not a function

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中文网2897 days ago444

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:15:40

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

    reply
    0
  • 阿神

    阿神2017-04-10 17:15:40

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

    reply
    0
  • 怪我咯

    怪我咯2017-04-10 17:15:40

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

    reply
    0
  • Cancelreply