javascript append() 不是函数 jQuery

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

append() is not a function jQuery

javascriptjquery

提问by Laurent Meyer

I'm quite new to JQuery (I come from Android World) and I do not understand why I'm stuck with Uncaught TypeError: $list.append is not a functionbecause I'm manipulating two jQuery Objects (as far as I know). So here is my code:

我对 JQuery 很陌生(我来自 Android World),我不明白为什么我会被困住,Uncaught TypeError: $list.append is not a function因为我正在操作两个 jQuery 对象(据我所知)。所以这是我的代码:

function transformInList(items, $list){
  if ($.isArray(items)){
      $.each(items, function(index, value){
          var $li = $('<li />');
          if (value.name && value.id){
              $li.attr('id', value.id);
              $li.text(value.name);
              $li.attr('class', 'clickableJobs')
              $list.append($li);
          }
      });
  }
}

This code is called with this function:

使用此函数调用此代码:

$(document).ready(function(){
    var $ul = ('<ul class="side-nav"></ul>');
    var jobs = {"jobs":[{"name":"Kellner", "id":"j10"}, {"name":"Fahrer", "id":"j11"}, {"name":"Lehrer", "id":"j12"}]};
    transformInList(jobs.jobs, $ul);
    $('div#jobColumn').append($ul)
});

My error should be trivial (as is my code ) but I do not get it.

我的错误应该是微不足道的(就像我的代码一样),但我不明白。

Thanks for help :D

感谢您的帮助:D

回答by Denys Séguret

Just a typo. You're passing a string, right now.

只是一个错字。你现在正在传递一个字符串。

Change

改变

var $ul = ('<ul class="side-nav"></ul>');

to

var $ul = $('<ul class="side-nav"></ul>');