javascript 为什么我的 JQuery 插件导致无法读取未定义的属性“长度”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18699968/
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
Why Is My JQuery Plugin Causing A Cannot Read Property 'length' of Undefined?
提问by VoidKing
I kind of feel bad asking yet another Cannot Read Property 'length' of Undefinedquestion, but no online (SE or otherwise) answers are in any way relating to my problem at hand.
我有点不好意思问另一个未定义问题的无法读取属性“长度”,但没有在线(SE 或其他)答案与我手头的问题有任何关系。
I have tried my best to narrow down why this script error is occurring for my situation, but I am at a loss.
我已尽力缩小我的情况出现此脚本错误的原因,但我不知所措。
If I exclude this tag in the page, the error ceases to show up:
如果我在页面中排除此标签,则错误不再显示:
<script src="~/Plugins/jquery-ui-1.10.3.drag-drop.min.js" type="text/javascript"></script>
All that the jquery-ui-1.10.3.drag-drop.min.js
file is is a jquery-custom
file I renamed, that includes jquery draggable, droppable, and sortable.
该jquery-ui-1.10.3.drag-drop.min.js
文件只是jquery-custom
我重命名的一个文件,其中包括 jquery 可拖放、可拖放和可排序。
I was sure that the error was caused by some script I wrote, but as I have just begun to test this out, I didn't have much script to 'comment out', and when I did so, it, to my befuddlement, didn't get rid of the error. The only way I have found so far to get rid of the error is to exclude the plug-in altogether, which I obviously wouldn't be able to use if I did.
我确信这个错误是由我写的一些脚本引起的,但是当我刚刚开始测试这个时,我没有太多的脚本可以“注释掉”,当我这样做时,让我感到困惑的是,没有摆脱错误。到目前为止,我发现摆脱错误的唯一方法是完全排除插件,如果我这样做,我显然无法使用。
The error in full as seen in Chrome's debugger:
在 Chrome 的调试器中看到的完整错误:
Uncaught TypeError: Cannot read property 'length' of undefined jquery-1.9.1.min.js:3
b.extend.each jquery-1.9.1.min.js:3
e.widget jquery-ui-1.10.3.drag-drop.min.js:6
(anonymous function) jquery-ui-1.10.3.drag-drop.min.js:6
(anonymous function) jquery-ui-1.10.3.drag-drop.min.js:6
The problem is (as it is with most errors like this) that I can't track down why this is causing an error, even if no code is written to even make use of this plug-in yet.
问题是(就像大多数这样的错误一样)我无法追踪为什么会导致错误,即使没有编写代码来使用这个插件。
I am using jquery-1.9.1.min.js
and the jquery-migrate-1.2.1.min.js
files.
我正在使用jquery-1.9.1.min.js
和jquery-migrate-1.2.1.min.js
文件。
Truth is, I could ignore this error, as it doesn't really break anything, and the dragging, dropping, and sortable features all seem to work fine and as expected. I just think it is ugly to have script errors in your page, and as I have never tolerated them in the past, I didn't really want to start tolerating them now.
事实是,我可以忽略这个错误,因为它并没有真正破坏任何东西,而且拖放和排序功能似乎都按预期工作正常。我只是认为在您的页面中出现脚本错误很丑陋,而且由于我过去从未容忍过这些错误,因此现在我真的不想开始容忍它们。
Am I barking up the wrong tree? Does the error not point to my drag-dropfile? What can I do to help narrow this error down?
我是不是叫错了树?错误不是指向我的拖放文件吗?我能做些什么来帮助缩小这个错误?
Please let me know if there is any more information I can supply that would help.
如果我可以提供更多有用的信息,请告诉我。
AS REQUESTED-<script>
tags as they appear in the <head>
Tag:
AS REQUESTED-<script>
出现在<head>
标签中的标签:
<script src="~/Resources/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="~/Resources/jquery-migrate-1.2.1.min.js" type="text/javascript"></script>
<script src="~/Plugins/jquery.maskedinput.min.js" type="text/javascript"></script>
<script src="~/Plugins/jquery-ui.min.js" type="text/javascript"></script>
<script src="~/Plugins/accounting.min.js" type="text/javascript"></script>
<script src="~/Plugins/jquery.autoellipsis-1.0.10.min.js" type="text/javascript"></script>
<script src="~/Plugins/jquery-ui-1.10.3.drag-drop.min.js" type="text/javascript"></script>
<script src="~/Scripts/PluginConfigurations.js" type="text/javascript"></script>
<script src="~/Scripts/main.js" type="text/javascript"></script>
<script src="~/Scripts/EditPages.js" type="text/javascript"></script>
回答by Denseflux
As @Pete has helped you identify, it is indeed caused by duplicating your jQuery UI core scripts. Load just them once, and your 'length of Undefined' issue will go away.
正如@Pete 已帮助您识别的那样,它确实是由重复您的 jQuery UI 核心脚本引起的。只加载一次,您的“未定义长度”问题就会消失。
回答by Karthik Kumar
As @Pete has helped you identify, it is indeed caused by duplicating your jQuery UI core scripts. Load just them once, and your 'length of Undefined' issue will go away.
正如@Pete 已帮助您识别的那样,它确实是由重复您的 jQuery UI 核心脚本引起的。只加载一次,您的“未定义长度”问题就会消失。
Including below file twice,will cause this issue.
包括以下文件两次,将导致此问题。
jquery.ui.core.min.js
jquery.ui.core.min.js
Make sure you have included only once.
确保您只包含一次。