javascript `find()` undefined 不是函数

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

`find()` undefined is not a function

javascriptjquery

提问by John Smith

I have this little jquery function:

我有这个小 jquery 函数:

$.each($('#TableBody tr:gt(0)'), function(index, element){
    element.find('td').last().html($('<span/>', {class: "input-group-addon", text: 'Ausw?hlen'}));
});

Somehow i get this error in the first line:

不知何故,我在第一行收到了这个错误:

Uncaught TypeError: undefined is not a function 

I tried some things in the console to fix this problem! But somehow nothing worked!

我在控制台中尝试了一些东西来解决这个问题!但不知何故没有任何效果!

Now i hope you can help me! JSFIDDLE: http://jsfiddle.net/3C98M/

现在我希望你能帮助我!JSFIDDLE:http: //jsfiddle.net/3C98M/

Thanks

谢谢

回答by adeneo

elementis not a jQuery object, the second argument of the $.eachfunction returns the native DOM node

element不是 jQuery 对象,$.each函数的第二个参数返回原生 DOM 节点

$.each($('#TableBody tr:gt(0)'), function(index, element){
    $(element).find('td').last().html($('<span/>', {"class": "input-group-addon", text: 'Ausw?hlen'}));
});

Also note that classis a reserved keyword in javascript and should be quoted.
It could also be written

另请注意,这class是 javascript 中的保留关键字,应加引号。
也可以这样写

$('#TableBody tr:gt(0)').each(function(index, element) {
      $(element).find('td').last().html(
          $('<span/>', {
              "class" : "input-group-addon", 
              text    : 'Ausw?hlen'
          })
      );
});

which is more readable IMO.

这是更具可读性的 IMO。

回答by theHacker

Surround element with $(...)to work

环绕元素与$(...)工作

$(element).find('td:last')....

回答by Ninh Pham

Wrap elementwith $()like the adeneo's answer or simply using this:

总结element$()像adeneo的回答,或者干脆利用this

$('#TableBody tr:gt(0)').each(function () {
    $(this).find('td:last').html($('<span/>', {
            "class" : "input-group-addon",
            text : 'Ausw?hlen'
        }));
}));

回答by Mdlc

This is most likely not the answer to question of John Smith (very generic name by the way) but it hopefully will be relevant to users looking for the solution to the 'X is not a function' javascript error (for which this post is the top result).

这很可能不是 John Smith 问题的答案(顺便说一下,这是一个非常通用的名称),但希望它与寻找“X 不是函数”javascript 错误的解决方案的用户相关(这篇文章是最高结果)。

It can be, that if you are loading multiple Javascript libraries, $ (shortcut for jQuery) get's overridden.

可能是,如果您正在加载多个 Javascript 库,$(jQuery 的快捷方式)会被覆盖。

You could use:

你可以使用:

 jQuery(element).find('td:last')....

Instead of

代替

 $(element).find('td:last')....

To solve this (or remove the library that overrides jQuery).

解决这个问题(或删除覆盖 jQuery 的库)。

Source: http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/

来源:http: //codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/