为什么jQuery Ajax .html不起作用?

success: function(json) {
            for (var i = 0; i < json.length; i++) {
        var response = json[i];
         $('#rv-container').html('<p>' + response.name + '</p>' + '<span>' + response.id + '</span>');
    }
        $('#history_tracking').hide();

        }

在success回调中,$(#rv-container).html()不起任何作用,但.prepend会起作用。

为什么jQuery在success回调中不允许 .html?通过ajax加载的html不断堆积已加载数据的顶部,因此,它不会替换#rv-container中当前的数据。

HTML:

<div id="bottom-toolbar">
  <div id="rv-facet">
    <div id="rv-fix">
      <li id="rv-title">
        Recently Viewed <span class="total_viewed"></span>
        <div id="rv-total" style="float:right; margin-right:25px;"></div>
      </li>
    </div>
    <div id="rv-content">
      <div id="rv-container">
        <div id="history_tracking"></div>
      </div>
    </div>
  </div>
</div>

有可以用prepend替换div的内容吗?

You are calling it in a loop, so obviously it makes a difference whether you are resetting the HTML content over and over (.html()) or prepending more and more to it (.prepend())?

Each iteration of your loop is defining the element's inner HTML.. So per step in the loop, this would basically be like var text = "this is a test", so that variable would never grow. You need to append or prepend the data.

success: function(json) {
            for (var i = 0; i < json.length; i++) {
        var response = json[i];
         $('#rv-container').append('<p>' + response.name + '</p>' + '<span>' + response.id + '</span>');
    }
        $('#history_tracking').hide();

        }