我使用JS委托进行工作。现在我想在点击事件中使用JSON成功$ .each值?



$(window).load(function() {
  $.ajax({
    url: '<?php echo base_url(); ?>' + 'main/data',
    dataType: "JSON",
    type: "POST",
    success: function(retdata) {
      $.each(retdata, function(i) {
        var content = '<div class="target btn-default">' + retdata[i].content;
        $("#article-data").append(content);
      });
    }
  });
});
//Using event delegate
$(function() {
  //Display dialog box when mouse click
  $("#article-data").on('click', ".target", function() {
    // retdata[i].content;
  });
});

<div id="article-data">

</div>

最佳答案

您可以使用jQuery保存数据:

$(window).load(function() {
  $.ajax({
    url: '<?php echo base_url(); ?>' + 'main/data',
    dataType: "JSON",
    type: "POST",
    success: function(retdata) {
      $.each(retdata, function(i) {
        var content = $('<div class="target btn-default"/>');
        content.data('retdata', retdata);
        content.html(retdata[i].content);
        content.appendTo($("#article-data"));
      });
    }
  });
});

//Using event delegate
//Display dialog box when mouse click
$("#article-data").on('click', ".target", function() {
    var data = $(this).data('retdata');
    console.log(data);
});

10-02 12:23