Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery循环通过API_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery循环通过API

Javascript 使用jQuery循环通过API,javascript,jquery,Javascript,Jquery,我试图使用MarvelAPI来显示角色参与的事件列表。这是我的剧本: $(".heroSubmit").click(function() { var heroName = $(".hero").val(); $.getJSON("http://gateway.marvel.com:80/v1/public/characters?name=" + heroName + "&apikey=", function (json) { $(".name").html(json.data.

我试图使用MarvelAPI来显示角色参与的事件列表。这是我的剧本:

$(".heroSubmit").click(function() {

var heroName = $(".hero").val();


$.getJSON("http://gateway.marvel.com:80/v1/public/characters?name=" + heroName + "&apikey=", function (json) {
  $(".name").html(json.data.results[0].name);
  $(".description").html(json.data.results[0].description);
  $(".image").attr("src", json.data.results[0].thumbnail.path + "/detail.jpg");
  $(".comics").html(json.data.results[0].comics.available);

  $.each( json.data.results[0].events.items, function(i, item) {
    $(".events").html("<li>" + item.name + "</li>");
  });

});

除了每一个部分外,一切都很好。现在,它只显示最后一个事件名称item.name。我需要显示所有事件

这是因为在每次迭代中都要重写事件的内容。要避免这种情况,请使用append替换html:


这是因为您在每次迭代中重写事件的内容。要避免这种情况,请使用append替换html:


是的,那会是个问题。谢谢你的帮助。是的,那会是个问题。谢谢你的帮助。
$.each( json.data.results[0].events.items, function(i, item) {
    $(".events").append("<li>" + item.name + "</li>");
});