jQuery 什么导致 TypeError: undefined is not a function?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10827035/
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
What causes TypeError: undefined is not a function?
提问by user1048676
All, I've got some code that I'm trying to debug and I used the following bit of code in my JS console:
所有,我有一些我正在尝试调试的代码,我在我的 JS 控制台中使用了以下代码:
clickEvents = $('ul.breadcrumb[data-allownextonly]').data("events").click;
jQuery.each(clickEvents, function(key, value) {
console.log(value.handler);
})
When I execute this I get the error: TypeError: undefined is not a function
当我执行此操作时,出现错误:TypeError: undefined is not a function
My question is what causes this to happen? From most of the things I read it deals with the fact that jQuery isn't loaded properly or there is a conflict between two scripts. jQuery is loading properly because I have other objects on my page that jQuery is working fine on. I'm using Wordpress so there is already no conflict.
我的问题是什么导致这种情况发生?从我读到的大部分内容来看,它涉及 jQuery 未正确加载或两个脚本之间存在冲突的事实。jQuery 正在正确加载,因为我的页面上还有其他 jQuery 可以正常处理的对象。我正在使用 Wordpress,所以已经没有冲突了。
In my console I changed my JS debugging to something like this:
在我的控制台中,我将 JS 调试更改为如下所示:
clickEvents = jQuery('ul.breadcrumb[data-allownextonly]').data("events").click;
jQuery.each(clickEvents, function(key, value) {
console.log(value.handler);
})
When I execute this I get the following error: TypeError: Cannot read property 'click' of undefined
当我执行此操作时,出现以下错误:TypeError: Cannot read property 'click' of undefined
Can anyone let me know what other reasons I would be getting these errors? If I have a conflicting script or something along those lines, how can I find out where the conflict is?
任何人都可以让我知道我会收到这些错误的其他原因吗?如果我有一个冲突的脚本或类似的东西,我怎么能找出冲突在哪里?
Thanks!
谢谢!
采纳答案by nbrooks
According to the jQuery API, the .data( key )
method returns undefined if the matched element doesn't have any data matching the key. Have a look at its usage http://api.jquery.com/data/. It only operates on the first element in the matched set in any case, so you cant use it to get an array of click event handlers. As is, it is saying that the first matched element has no data attribute "events". Once you get the data that is attached, do
根据 jQuery API,.data( key )
如果匹配的元素没有任何与键匹配的数据,则该方法返回 undefined。看看它的用法http://api.jquery.com/data/。在任何情况下,它都只对匹配集合中的第一个元素进行操作,因此您不能使用它来获取单击事件处理程序的数组。也就是说,第一个匹配的元素没有数据属性“事件”。获得附加的数据后,请执行
var clickEvents = [];
$( selector ).data( key ).each( function() {
clickEvents.push( $(this).click );
});
to collect the click events.
收集点击事件。
回答by Jeremy Holovacs
You get that error when you try to execute a function that is uninitialized or improperly initialized.
当您尝试执行未初始化或未正确初始化的函数时,您会收到该错误。
回答by Ja?ck
It would seem that jQuery('ul.breadcrumb[data-allownextonly]').data("events")
is undefined
, so any dereferencing on that raises an error.
似乎jQuery('ul.breadcrumb[data-allownextonly]').data("events")
是undefined
,因此任何对它的取消引用都会引发错误。
Btw, changing $
to jQuery
won't change much in your code. If jQuery wasn't loaded, it would give a different error.
顺便说一句,更改$
为jQuery
不会在您的代码中发生太大变化。如果未加载 jQuery,则会出现不同的错误。