我正在寻找一种方法,每5秒刷新一次div而不刷新整个页面。
我有一个名为mod_chat.php的页面,我将其包含在我的页面profile.php中,所以:
<?php include('includes/mod_profile/mod_chat/mod_chat.php'); ?>
mod_chat.php中的是我想每5秒重新加载一次的div,但是没有刷新整个页面,我尝试的一切都没有用,请有人告诉我如何解决这个问题。
这是我尝试过的:
<div class="chat_box">
<div class="chat_box_head"><div class="chat_box_head_text">You and <?php
$chat_set_name = chat_set_name();
while ($chat1 = mysql_fetch_array($chat_set_name)) { ?><?php echo "".$chat1['display_name']."" ?><? } ?></div><div class="chat_close"></div></div>
<div class="chat_content">
<?php
$chat_set = get_chats_to();
while ($chat = mysql_fetch_array($chat_set)) {
if (isset($_SESSION['user_id'])) {
if ($chat['to_user_id'] == $_SESSION['user_id']){ ?>
<?php echo "<div class=\"chat_row\">".$chat['content']."<br/><a href=\"#\">{$chat['display_name']}</a></div>"; ?>
<? } } }?>
</div>
的javascript:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('.chat_box').load('mod_chat.php');
}, 05000); // refresh every 05000 milliseconds
</script>
答案 0 :(得分:2)
你可以做到这一点;
function get_chat() {
$.get('/mod_chat.php', {}, function(data ) {
$("#div").html(data);
//optional code, if you wanted the chat box to have a scroller and automatically move to bottom when messages are loaded.
var textarea = document.getElementById('divid');
textarea.scrollTop = textarea.scrollHeight;
});
//load messages every 4 seconds
setInterval("get_chat();", 4000);
}
然后在文档加载中,您将调用该函数;
$(document).ready(function(){
get_chat();
});