通过AJAX重新加载div内容后,更新的html消失了

时间:2015-04-16 09:15:10

标签: javascript jquery html ajax

我有JS代码,每2秒重新加载一次div:

var auto_refresh = setInterval(function() {
    $('#indexRefresh').load('/includes/index_refresh_include.php?_=' + Math.random());
}, 2000);

然后我发出一个AJAX请求,当按下“加载更多”按钮时会加载更多内容,但是因为它正在重新加载加载的内容消失。

$(function() {
    $(document).on("click", ".loadmore", function() {
        var ID = $(this).attr("id");
        var split = ID.split("-");
        if (ID) {
            $("#more" + ID).html('<img src="moreajax.gif" />');
            $.ajax({
                type: "POST",
                url: "loadmore.php",
                data: "rowstart=" + ID,
                cache: false,
                success: function(html){
                    $("#updates").append(html);
                    $("#more" + ID).remove();
                }
            });
        }
        else {
            $(".morebox").html('The End');
        }
        return false;
    });
});

我应该怎么做,以便加载的内容不会消失?

编辑:

这是html

echo"<div id='indexRefresh'>";
        include INC.'index_include.php';
echo"</div>";

这是index_refresh_include.php文件

$result1 = dbquery("SELECT DISTINCT model_id, model_username FROM models LEFT JOIN `show` ON models.model_id=show.show_model_id ORDER BY show_ended=0 DESC, model_id ASC LIMIT 6");

$rowstart = 0;
echo"<section class='list'>";
echo"    <div id='updates'>";
echo"        <div class='small_block'>";
while($data = dbarray($result1)){
    $modelID = $data['model_id'];
    $result = dbquery("SELECT * FROM `show` WHERE show_model_id='$modelID'");
    if(dbrows($result) >= 5){
        echo"    <article>";
    } else {
        echo"    <article class='new'>";
    }
    echo"           <a class='checkcam' href='checkcam.php?model=".$data['model_username']."'>";
    echo"               <img src='".getAvatar($data['model_id'])."' alt='' />";
    echo"           </a>";
    echo"       </article>";
    $rowstart++;
}
echo"        </div>";

$blockstart = 0;
$large = dbquery("SELECT * FROM largeblocks WHERE large_status=1 ORDER BY large_pos ASC LIMIT 1 OFFSET $blockstart");
if(dbrows($large) != 0){
    while($block = dbarray($large)){
        echo"        <div class='large_block'>";
        echo"            <article>";
        echo"                <a href='Cam.php?show=1'>";
        echo"                    <img src='http://placehold.it/600x600' alt='' />";
        echo"                </a>";
        echo"            </article>";
        echo"        </div>";
        $blockstart++;
    }
} else {
    $model = dbquery("SELECT * FROM models ORDER BY model_credits DESC LIMIT 1 OFFSET $blockstart");
    while($block = dbarray($model)){
        echo"        <div class='large_block'>";
        echo"            <article>";
        echo"               <a class='checkcam' href='checkcam.php?model=".$block['model_username']."'>";
        echo"                   <img src='".getAvatar($block['model_id'], '600x600')."' alt='' />";
        echo"                </a>";
        echo"            </article>";
        echo"        </div>";
        $blockstart++;
    }
}

echo"        <br class='clear' />";

$large = dbquery("SELECT * FROM largeblocks WHERE large_status=1 ORDER BY large_pos ASC LIMIT 1 OFFSET $blockstart");
if(dbrows($large) != 0){
    while($block = dbarray($large)){
        echo"        <div class='large_block'>";
        echo"            <article>";
        echo"                <a href='Cam.php?show=1'>";
        echo"                    <img src='http://placehold.it/600x600' alt='' />";
        echo"                </a>";
        echo"            </article>";
        echo"        </div>";
        $blockstart++;
    }
} else {
    $model = dbquery("SELECT * FROM models ORDER BY model_credits DESC LIMIT 1 OFFSET $blockstart");
    while($block = dbarray($model)){
        echo"        <div class='large_block'>";
        echo"            <article>";
        echo"               <a class='checkcam' href='checkcam.php?model=".$block['model_username']."'>";
        echo"                   <img src='".getAvatar($block['model_id'], '600x600')."' alt='' />";
        echo"                </a>";
        echo"            </article>";
        echo"        </div>";
        $blockstart++;
    }
}

$result1 = dbquery("SELECT DISTINCT model_id, model_username FROM models LEFT JOIN `show` ON models.model_id=show.show_model_id ORDER BY show_ended=0 DESC, model_id ASC LIMIT 6 OFFSET $rowstart");

echo"        <div class='small_block'>";
while($data = dbarray($result1)){
    $modelID = $data['model_id'];
    $result = dbquery("SELECT * FROM `show` WHERE show_model_id='$modelID'");
    if(dbrows($result) >= 5){
        echo"    <article>";
    } else {
        echo"    <article class='new'>";
    }
    echo"               <a class='checkcam' href='checkcam.php?model=".$data['model_username']."'>";
    echo"                   <img src='".getAvatar($data['model_id'])."' alt='' />";
    echo"               </a>";
    echo"           </article>";
    $rowstart++;
}


echo"        </div>";

echo"        <br class='clear' />";

echo"    </div>";

echo"    <div id='more$rowstart-$blockstart' class='morebox'>";
echo"        <a href='#' class='loadmore gardient' id='$rowstart-$blockstart'>".$lang['index_loadmore']."</a>";
echo"    </div>";

echo"</section>";

1 个答案:

答案 0 :(得分:2)

这是正确的代码

var auto_refresh = setInterval(function() {
    $('<div></div>').appendTo('#indexRefresh').load('/includes/index_refresh_include.php?_=' +Math.random());
}, 2000);

每次加上&lt; div>在id = indexRefresh的div上。然后在新div中加载URL

请参阅fiddle