javascript - 如何让加载更多数据附加到顶部?

标签 javascript php jquery html

我已完成加载更多结果,但加载结果将附加在底部。我如何让它附加在顶部?

因为我发送的消息是放在底部的,所以我希望将旧消息附加到顶部。

这是我的 html:

<form name ="chatroom">

<div class="count-user-online">
<?php echo $row ?>
</div>
<div class="chatroom-upper-container" id="chatroom-upper-container">
<input type="hidden" id="result_no" value="10">
    <div id="inner">
        Loading Message....<img src="../images/loading.gif"/>
    </div>


</div>
     <input type="button" id="load" value="Load More Results">
<div class="chatroom-lower-left-container">
    <textarea class="message-setting" id="area-message" placeholder="type text" name= "msg"></textarea>
</div><div class="chatroom-lower-right-container">
    <button type="button" class="btn sendmessage-btn" onclick= "submitChat()">Send</button>
</div>

</form>

这是我的js:

$(document).ready(function(){
 $("#load").click(function(){
  loadmore();
 });
});

function loadmore()
 {
   var val = document.getElementById("result_no").value;
 $.ajax({
 type: 'post',
 url: 'chatloadmore.php',
   data: {
  getresult:val
     },
   success: function (response) {
   var content = document.getElementById("chatroom-upper-container");
   content.innerHTML = content.innerHTML+response;

  // We increase the value by 2 because we limit the results by 2
  document.getElementById("result_no").value = Number(val)+10;
 }
});
}

这是我的 loadmorefile.php:

<?php

  include '../config.php';
  include'login.php';

  $no = $_POST['getresult'];
  $username = $_SESSION['username'];

  $sql1= "SELECT * FROM (
   SELECT * FROM logs ORDER BY id DESC LIMIT $no,40
   ) sub
   ORDER BY id ASC ";

   $result1 = mysqli_query($connection,$sql1);

   while($extract = mysqli_fetch_array($result1)){
    $color = ($extract['username'] == $username) ? '    #F5F5F5' :  '#DCDCDC';
    $position = ($extract['username'] == $username) ? 'right' : 'left';
    echo "
      <div class='left-wrap-message' style='background-color:$color; float:$position;'>
      <p style='text-align:$position; margin:0;'>". $extract['username']. "  : </p> 
      <p style='text-align:$position; margin:0; text-align:left;'>  " . $extract['msg']. "</p></div>
       <div class='msg-dateandtime' style='text-align:$position;'> "  . $extract['date']. "</div>";
     }

    ?>

最佳答案

代替

content.innerHTML = content.innerHTML+response;

做一个

content.innerHTML = response + content.innerHTML;

关于javascript - 如何让加载更多数据附加到顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372000/

相关文章:

javascript - 调整旋转对象的大小保留左上角(亚像素渲染问题)

javascript - 在pdf中使用jspdf.debug.js Wrap table head时

php - 谷歌图表似乎不起作用

php - 如何提高 MySQL INSERT 性能?

javascript - 钛动画不保留其值(value)

javascript - 将元素动态添加到 SVG,元素添加到 HTML DOM 但未呈现

php - 每次添加新行时更新列表

javascript - 只要动画有效,就试图停止 jQuery 单击事件

javascript - jsPlumb如何去除重复的连接

javascript - 更改下拉列表中的选择时更改 Highcharts 中的数据