JQuery - 使用.innerHTML附加而不是替换内容?

时间:2010-11-08 13:48:27

标签: javascript jquery

下面是一个函数,它一次检索16个用户较旧的wallposts,并将每个16块附加到名为“sw1”的div中当前列表的末尾。

它工作正常,除了当前有一个包含当前正在播放的视频/嵌入对象的wallpost的情况。 当我们在sw1中播放对象时单击调用下面函数的按钮时,下面的函数似乎会导致sw1的整个内容被删除,然后重新转储。

是否可以修改下面的函数,以便它只是将新数据附加到sw1的末尾,而不是替换整个内容,杀死当前正在播放的任何对象?

function fetch_older_wallposts(u,lim){
var xhr = getXMLHttpRequest();

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {

//document.getElementById( 'sw1' ).innerHTML.append( xhr.responseText );
document.getElementById( 'sw1' ).innerHTML += xhr.responseText;
}else{
document.getElementById( 'oaps_loading' ).innerHTML = 'loading';
}
};

xhr.open("POST", "script.php?u="+u+"&lim="+lim, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(null);
}

任何形式的帮助或指导都会受到赞赏......

感谢所有有用的评论家伙!

2 个答案:

答案 0 :(得分:6)

如果你打算使用jQuery,为什么混合和匹配样式,一直使用jQuery:

function fetch_older_wallposts(u,lim){
    $('#oaps_loading').html('loading');
    $.ajax({url: 'script.php', data: { u: u, lim: lim}, type: 'POST', 
        success: function(data) {
            $('#sw1').append(data);
        });
}

答案 1 :(得分:3)

致电$('#sw1').append(xhr.responseText);

相关问题