简体   繁体   English

jQuery插件返回“无法读取未定义的属性”

[英]jQuery plugin returning “Cannot read property of undefined”

I'm trying to do something i've done numerous times. 我正在尝试做很多次我做过的事情。 I can't figure out why this isn't working. 我无法弄清楚为什么这不起作用。 No matter how i write the jQuery code, it doesn't work. 无论我如何编写jQuery代码,它都不起作用。 menuitems[i].action() just does NOT work. menuitems[i].action()只是不起作用。 Below is Example 1 in which this example, no matter what item is clicked it returns the last item's action (in this example it's the alert('Forward!') ). 下面是示例1 ,其中此示例,无论单击哪个项目,它都会返回最后一项的操作(在此示例中,它是alert('Forward!') )。 The 2nd returns undefined property. 第二个返回undefined属性。 The full error below. 完整的错误如下。

My jQuery plugin is called like this (examples below are what happen with this same call): 我的jQuery插件就是这样调用的(以下示例是同一个调用发生的事情):

$('p').contextMenu([
    {
        name:'Back',
        action:function(){
            alert('Back!');
        },
        icon:'http://cdn.iconfinder.net/data/icons/crystalproject/16x16/actions/agt_back.png'
    },
    {
        name:'Forward',
        action:function(){
            alert('Forward!');
        },
        icon:'http://cdn.iconfinder.net/data/icons/crystalproject/16x16/actions/agt_forward.png'
    }
]);

Example 1: 例1:

for (i in menuitems){
    $('<li/>',{
        'html':'<img src="'+menuitems[i].icon+'">'+menuitems[i].name,
        'class':o.itemClass,
        'click':function(){
            menuitems[i].action();
        }
    }).appendTo('.'+o.listClass);
}

This returns an alert with Forward! 这将返回一个带有Forward的警报! no matter which item, Back or Forward, is clicked. 无论点击哪一项,后退或前进。

Example 2: 例2:

var len = menuitems.length;
for (var i = 0; i < len; i++){
    $('<li/>',{
        'html':'<img src="'+menuitems[i].icon+'">'+menuitems[i].name,
        'click':function(){
            menuitems[i].action();
        },
        'class':o.itemClass
    }).appendTo('.'+o.listClass);
}

I get: 我明白了:

Uncaught TypeError: Cannot read property 'action' of undefined 未捕获的TypeError:无法读取未定义的属性“action”

Other random things i tried were detaching the click and reattaching it outside of that appendTo() block, changed action() to newtest() to make sure it wasn't conflicting with any built in keywords. 我试过的其他随机的东西是分离点击并将其重新附加到appendTo()块之外,将action()更改为newtest()以确保它与任何内置关键字没有冲突。 I also tried doing a $('body').append('<li>{blah blah}</li>').find('li').click(function(){/*blah blah*/}); 我也试过做一个$('body').append('<li>{blah blah}</li>').find('li').click(function(){/*blah blah*/}); but it still returned the same thing. 但它仍然返回相同的东西。 I'm outta ideas! 我想法了!

The problem is that "i" is incremented, so by the time the click event is executed the value of i equals len. 问题是“i”递增,所以在执行click事件时,i的值等于len。 One possible solution is to capture the value of i inside a function: 一种可能的解决方案是在函数内捕获i的值:

var len = menuitems.length;
for (var i = 0; i < len; i++){
    (function(i) {
      $('<li/>',{
          'html':'<img src="'+menuitems[i].icon+'">'+menuitems[i].name,
          'click':function(){
              menuitems[i].action();
          },
          'class':o.itemClass
      }).appendTo('.'+o.listClass);
    })(i);
}

In the above sample, the anonymous function creates a new scope which captures the current value of i, so that when the click event is triggered the local variable is used instead of the i from the for loop. 在上面的示例中,匿名函数创建一个新范围,用于捕获i的当前值,以便在触发click事件时使用局部变量而不是for循环中的i。

I had same problem with 'parallax' plugin. 我对'parallax'插件有同样的问题。 I changed jQuery librery version to *jquery-1.6.4* from *jquery-1.10.2*. 我从*jquery-1.10.2*.将jQuery librery版本更改为*jquery-1.6.4* *jquery-1.10.2*. And error cleared. 错误已清除。

Usually that problem is that in the last iteration you have an empty object or undefine object. 通常问题是,在最后一次迭代中,您有一个空对象或未定义对象。 use console.log() inside you cicle to check that this doent happend. 在你的cicle中使用console.log()来检查这个事件是否发生。

Sometimes a prototype in some place add an extra element. 有时,某个地方的原型会添加额外的元素。

暂无
暂无

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

相关问题 jQuery TableSorter插件错误:无法读取未定义的属性“ 1” - jQuery TableSorter Plugin error : cannot read property '1' of undefined JQuery插件中的this.selector-无法读取未定义的属性“ top” - this.selector in JQuery plugin - Cannot read property 'top' of undefined 为什么我的JQuery插件导致无法读取未定义的属性“长度”? - Why Is My JQuery Plugin Causing A Cannot Read Property 'length' of Undefined? JQuery DataTables和ColVis插件出错“无法读取未定义的属性&#39;s宽度&#39; - Error with JQuery DataTables and ColVis plugin “Cannot read property 'sWidth' of undefined” 无法读取 jquery 中未定义的属性“0” - Cannot read property '0' of undefined in jquery 尽管返回了Promise,但“无法读取未定义的属性&#39;then&#39;” - “cannot read property 'then' of undefined”, despite returning a Promise 返回一个Promise,但是得到“无法读取未定义的属性&#39;then&#39;” - Returning a Promise, but getting 'Cannot read property 'then' of undefined' 返回的appendChild无法读取未定义的属性&#39;containerDiv&#39; - appendChild returning Cannot read property 'containerDiv' of undefined JavaScript插件-无法读取未定义的属性“窗口” - JavaScript plugin - cannot read property 'window' of undefined 使用 jquery 表单验证插件时无法读取未定义的属性“调用” - Cannot read property 'call' of undefined while using jquery form validation plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM