简体   繁体   English

TypeError:未定义不是jQuery函数

[英]TypeError: undefined is not a function jQuery

I'm new to jQuery and I can get it to sometimes work, however, for some reason, when I try to call a function, it gives me the title error, but if I do it in developer tools, it works fine. 我是jQuery的新手,有时可以使它正常工作,但是由于某种原因,当我尝试调用一个函数时,它给了我标题错误,但是如果我在开发人员工具中这样做,它就可以正常工作。

http://jsfiddle.net/otanan/pmzzLo3e/#&togetherjs=AezijhfBrj http://jsfiddle.net/otanan/pmzzLo3e/#&togetherjs=AezijhfBrj

It seems to work fine when retrieving the classes from the DOM, but not when I call a function such as 从DOM检索类时,它似乎工作正常,但是当我调用诸如

.click(function() {});

Here's the code: 这是代码:

var downloads = $(".info"),
    className = "info_clicked";

for(var i in downloads)
{
    downloads[i].click(function()
    {
        if(!this.hasClass(className))
            this.addClass(className);
        else
            this.removeClass(className);
    });
}

When you access a jQuery collection as an array, it returns the DOM elements, not jQuery objects. 当您以数组形式访问jQuery集合时,它将返回DOM元素,而不是jQuery对象。 You should use .each() rather than for (i in downloads) : 您应该使用.each()而不是for (i in downloads)

downloads.each(function() {
    $(this).click(function() {
        if (!$(this).hasClass(className)) {
            $(this).addClass(className);
        } else {
            $(this).removeClass(className);
        }
    });
});

You could also simplify the whole thing to: 您还可以将整个过程简化为:

downloads.click(function() {
    $(this).toggleClass(className);
});

Most jQuery methods automatically iterate over all the elements in a collection if it makes sense to do so (the notable exceptions are methods that return information from the element, like .text() or .val() -- they just use the first element). 如果.val()话,大多数jQuery方法会自动遍历集合中的所有元素(值得注意的例外是从元素中返回信息的方法,例如.text().val() -它们仅使用第一个元素) )。 So you generally only have to iterate explicitly if you need to do different things for each element. 因此,通常只需要在需要对每个元素执行不同操作的情况下进行显式迭代即可。 This is one of the great conveniences of using jQuery rather than plain JS: you rarely have to write explicit iterations. 这是使用jQuery而不是普通JS的最大便利之一:您几乎不必编写显式迭代。

I think the issue is that you're attempting to call a jQuery function on an object that is no longer a jQuery object. 我认为问题在于您正试图在不再是jQuery对象的对象上调用jQuery函数。

For example you're saying $(".info") . 例如,您说的是$(".info") Which retrieves a single jQuery object. 检索单个jQuery对象。 As soon as you index that object downloads[i] it is no longer a jQuery object, it is a plain HTML element and does not have a click function available. 一旦索引了该对象downloads[i]索引downloads[i]它就不再是jQuery对象,它是纯HTML元素,并且没有可用的click函数。

What you really need to do is get the jQuery object for the indexed item: 您真正需要做的是获取索引项目的jQuery对象:

var downloads = $(".info"),
className = "info_clicked";

for(var i = 0; i < downloads.length; i++)
{
    $(downloads[i]).click(function()
    {
        if(!this.hasClass(className))
            this.addClass(className);
        else
            this.removeClass(className);
    });
 }

试试吧:

$(downloads[i]).click(function(){ //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM