Javascript 打开新的基础项目时出现“Uncaught TypeError: a.indexOf is not a function”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38871753/
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-23 21:58:35  来源:igfitidea点击:

"Uncaught TypeError: a.indexOf is not a function" error when opening new foundation project

javascriptjquerycssgoogle-chromezurb-foundation

提问by FreddieE

I've created a new Foundation 5 project through bash, with foundation new my-project. When I open the index.html file in Chrome an Uncaught TypeError: a.indexOf is not a functionerror is shown in the console, originating in jquery.min.js:4.

我通过 bash 创建了一个新的 Foundation 5 项目,使用foundation new my-project. 当我在 Chrome 中打开 index.html 文件时,Uncaught TypeError: a.indexOf is not a function控制台中会显示一个错误,源自jquery.min.js:4.

I created the project following the steps on the foundation site, but I can't seem to get rid of this error. Foundation and jQuery look like they are included and linked up correctly in the index.html file, and the linked app.js file is including $(document).foundation();

我按照基础站点上的步骤创建了该项目,但似乎无法摆脱此错误。Foundation 和 jQuery 看起来像是在 index.html 文件中包含并正确链接,并且链接的 app.js 文件包含$(document).foundation();

Does anyone know what is causing this error? and what a solution might be?

有谁知道是什么导致了这个错误?什么解决方案可能是?

Console error message screenshot

控制台错误信息截图

回答by Daniel Llano

This error might be caused by the jQuery event-aliases like .load(), .unload()or .error()that all are deprecatedsince jQuery 1.8. Lookup for these aliases in your code and replace them with the .on()method instead. For example, replace the following deprecated excerpt:

此错误可能是由 jQuery 事件别名(如 )引起的.load().unload()或者自 jQuery 1.8 以来.error()都已弃用。在您的代码中查找这些别名并将它们替换为方法。例如,替换以下已弃用的摘录:.on()

$(window).load(function(){...});

with the following:

具有以下内容:

$(window).on('load', function(){ ...});

回答by shaune

This error is often caused by incompatiblejQuery versions. I encountered the same error with a foundation 6repository. My repository was using jQuery 3, but foundation requires an earlier version. I then changed it and it worked.

此错误通常是由不兼容的jQuery 版本引起的。我在foundation 6存储库中遇到了同样的错误。我的存储库正在使用jQuery 3,但基础需要早期版本。然后我改变了它并且它起作用了。

If you look at the version of jQuery required by the foundation 5 dependenciesit states "jquery": "~2.1.0".

如果您查看基础 5 依赖项所需的 jQuery 版本,它会指出"jquery": "~2.1.0"

Can you confirm that you are loading the correct version of jQuery?

您能确认您正在加载正确版本的 jQuery 吗?

I hope this helps.

我希望这有帮助。

回答by Govarthanan Venunathan

Please add below jQuery Migrate Plugin

请在下面添加 jQuery Migrate 插件

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.4.1.min.js"></script>

回答by Harshit Pant

I faced this issue too. I was using jquery.poptrox.min.jsfor image popping and zooming and I received an error which said:

我也遇到过这个问题。我正在使用jquery.poptrox.min.js图像弹出和缩放,但收到一个错误消息:

“Uncaught TypeError: a.indexOf is not a function”error.

“未捕获的类型错误:a.indexOf 不是函数”错误。

This is because indexOfwas not supported in 3.3.1/jquery.min.jsso a simple fix to this is to change it to an old version 2.1.0/jquery.min.js.

这是因为indexOf它不受支持,3.3.1/jquery.min.js因此一个简单的修复方法是将其更改为旧版本2.1.0/jquery.min.js

This fixed it for me.

这为我修好了。

回答by T.Todua

One of the possible reasons is when you load jQuery TWICE,like:

可能的原因之一是当您加载 jQuery TWICE 时,例如:

<script src='..../jquery.js'></script>
....
....
....
....
....
<script src='......./jquery.js'></script>

So, check your source code and remove duplicate jQuery load.

因此,请检查您的源代码并删除重复的 jQuery 负载。

回答by Ricky Levi

I'm using jQuery 3.3.1and I received the same error, in my case, the URL was an Objectvs a string.

我正在使用jQuery 3.3.1并且收到相同的错误,在我的情况下,URL 是Object一个字符串。

What happened was, that I took URL = window.location- which returned an object. Once I've changed it into window.location.href- it worked w/o the e.indexOferror.

发生的事情是,我拿走了URL = window.location- 它返回了一个对象。一旦我将其更改为window.location.href- 它就可以正常工作而没有e.indexOf错误。

回答by Ntiyiso Rikhotso

I solved this by installing the correct version of Jquerythat my project required using npm

我通过安装Jquery我的项目所需的正确版本来解决这个问题npm