为什么 jQuery 会抛出错误“fadeOut 不是函数”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/934446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:10:43  来源:igfitidea点击:

Why does jQuery throw the error `fadeOut is not a function'?

jquery

提问by Marc V

I am using jQuery and put this code in my javascript:

我正在使用 jQuery 并将此代码放入我的 javascript 中:

function HideMe(itemID) {
    var myDiv = 'item_' + itemID;
    $(myDiv).fadeOut("slow");
}

But its giving me this error: fadeOutis not a function.

但它给了我这个错误:fadeOutis not a function

采纳答案by Kobi

Do you have another javascript library on that page? It seems you have the hidefunction, and the $defined (prototype, for example, also has an hidefunction).
If that is the case, try:

你在那个页面上有另一个 javascript 库吗?看来你有这个hide功能,并且$定义了(例如,原型也有一个hide功能)。
如果是这种情况,请尝试:

jQuery("#item_0").fadeOut("slow");

回答by Jon Schneider

This will happen if you're using the "slim" version of jQuery. Only the "full" version of jQuery includes animation effects.

如果您使用的是“纤薄”版本的 jQuery,就会发生这种情况。只有“完整”版本的 jQuery 包含动画效果。

Try grabbing the "full" version of jQuery from the jQuery downloads pageand including that in your page (or including a full version of jQuery from a CDNfrom your page).

尝试从jQuery 下载页面获取 jQuery 的“完整”版本并将其包含在您的页面中(或从您页面的 CDN 中包含完整版本的jQuery)。

回答by rychrist88

I had this error because I was using a slim version of jQuery. If you download the full version you should be okay.

我遇到这个错误是因为我使用的是精简版的 jQuery。如果你下载完整版,你应该没问题。

回答by cgp

Even if the selector didn't return any items in the collection the function call would have worked (not generated this error anyway) if jQuery was loaded correctly.Either there is a conflict in the page, or it didn't load at all. You can try

即使选择器没有返回集合中的任何项目,如果 jQuery 被正确加载,函数调用也会起作用(无论如何不会产生这个错误)。页面中存在冲突,或者根本没有加载。你可以试试

jQuery(myDiv).fadeOut("slow");

or look into why jQuery hasn't been loaded.

或者看看为什么 jQuery 没有被加载。

P.S.: don't forget the #in the selector if selecting by id.

PS:#如果按 id 选择,请不要忘记选择器中的 。

回答by moff

Also, you probably forgot a #in the selector (unless you've got something like <item_1 />in the markup).

此外,您可能忘记了#选择器中的 a(除非您<item_1 />在标记中使用了类似的东西)。

var myDiv = '#item_' + itemID;

jQuery uses CSS selectors to search for elements, so without the #, you'd get every element with the tag item_xinstead of the ID.

jQuery 使用 CSS 选择器来搜索元素,因此如果没有#,您将获得带有标记item_x而不是 ID 的每个元素。

回答by Mesh

It looks like jquery is not correctly attached to the page.

看起来 jquery 没有正确附加到页面。

Check your linking to jQuery.

检查您与 jQuery 的链接。

回答by Sudhir Jonathan

Try keeping it inside

试着把它放在里面

$(document).ready(function(){
// your code. and don't forget the '#' in front of item.
});

Looks like you're trying to call the function before jQuery / the DOM loads.

看起来您正在尝试在 jQuery / DOM 加载之前调用该函数。

回答by Tomas Aschan

You have liDivinstead of myDiv. Try:

你有liDiv而不是myDiv. 尝试:

function HideMe(itemID) {
    var myDiv = 'item_' + itemID;
    $(myDiv).fadeOut("slow");
}

回答by Mark Zee

On moving a .html template to a wordpress one, I found this popping up regularly.

在将 .html 模板移动到 wordpress 模板时,我发现它会定期弹出。

Two reasons, and the console error can be deceptive:

两个原因,控制台错误可能具有欺骗性:

  1. The error in the console can send you down the wrong path. It MIGHT not be the real issue. First reason for me was the orderin which you have set your .js files to load. In html easy, put them in the same order as the theme template. In Wordpress, you need to enqueue them in the right order, but also set a priority if they don't appear in the right order,

  2. Second thing is are the .js files in the header or the footer. Moving them to the footer can solve the issue - it did for me, after a day of trying to debug the issue. Usually doesn't matter, but for a complex page with lots of js libraries, it might!

  1. 控制台中的错误可能会让您走上错误的道路。这可能不是真正的问题。我的第一个原因是您设置 .js 文件加载的顺序。在 html easy 中,将它们按与主题模板相同的顺序排列。在 Wordpress 中,您需要以正确的顺序将它们入队,但如果它们没有以正确的顺序出现,也要设置优先级,

  2. 第二件事是页眉或页脚中的 .js 文件。将它们移到页脚可以解决问题 - 经过一天的尝试调试问题后,它对我来说确实如此。通常无关紧要,但对于具有大量 js 库的复杂页面,它可能会!