jQuery和AJAX加载html,选择其中的元素,并附加到当前DOM

jQuery和AJAX加载html,选择其中的元素,并附加到当前DOM,jquery,ajax,Jquery,Ajax,我正在开发一个jQuery脚本,从下一页加载html并将其附加到当前容器中。基本上做一个永无止境的页面。但是,函数返回时出错,尽管error抛出为空。这是通过ajax获取html并将其附加到当前DOM的正确方法吗 $.ajax({type : "GET", url : currentUrl + "/page/" + nextPageNumber, dataType : "html", error : function(jqXHR, textStat

我正在开发一个jQuery脚本,从下一页加载html并将其附加到当前容器中。基本上做一个永无止境的页面。但是,函数返回时出错,尽管error抛出为空。这是通过ajax获取html并将其附加到当前DOM的正确方法吗

$.ajax({type : "GET",
        url : currentUrl + "/page/" + nextPageNumber,
        dataType : "html",
        error : function(jqXHR, textStatus, errorThrown) { alert(textStatus); alert(errorThrown); },
        success : function(data) {
            var frag = document.createDocumentFragment();
            frag.appencChild($(data)[0]);
            var $page = $(frag); 
            $("div.blueline-page-container").append($page.find('div.blueline-page-container').html());
            nextPageNumber++;
        }
});
你为什么不使用:

$selector.loadhtmlhere

它可以很好地与html配合使用。

为什么不使用:

$selector.loadhtmlhere


它可以很好地与html配合使用。

首先,没有appencChild这样的东西。如果您试图加载一个ID,那么您已经可以通过使用$.load的URL字符串中的whatever.htmlyurid来指定一个片段。如果您有更具体的需求,只需将响应HTML传递给jQuery,并将其作为普通的jQuery对象进行操作:

...
success : function(data) {
    var $page = $(data);
    $("div.blueline-page-container").append($page.find('div.blueline-page-container').html());
}
...

首先,没有“ApprencChild”这样的东西。如果您试图加载一个ID,那么您已经可以通过使用$.load的URL字符串中的whatever.htmlyurid来指定一个片段。如果您有更具体的需求,只需将响应HTML传递给jQuery,并将其作为普通的jQuery对象进行操作:

...
success : function(data) {
    var $page = $(data);
    $("div.blueline-page-container").append($page.find('div.blueline-page-container').html());
}
...

感谢所有回答的人,这真的帮助我找到了正确的方向。对于那些在搜索中偶然发现这一点的人,我最终是这样做的:

$("div.blueline-page-container").append("<div id='blueline-page-" + nextPageNumber + "'><div>");
$("#blueline-page-" + nextPageNumber).load("/page/" + nextPageNumber + " div.blueline-page-container");
我使用唯一的id向页面添加一个,然后将ajax结果加载到创建的页面


最后很简单…

感谢所有回答的人,这真的帮我指明了正确的方向。对于那些在搜索中偶然发现这一点的人,我最终是这样做的:

$("div.blueline-page-container").append("<div id='blueline-page-" + nextPageNumber + "'><div>");
$("#blueline-page-" + nextPageNumber).load("/page/" + nextPageNumber + " div.blueline-page-container");
我使用唯一的id向页面添加一个,然后将ajax结果加载到创建的页面


最后很简单…

好的,我已经阅读了相关文档,看起来这正是我需要的。我实施了,但仍然没有工作,所以我又去钓鱼了。我发现我的currentUrl仅返回/I使用currentUrl=window.location.pathname;获取url。有什么想法吗?找到了。我需要使用主机名而不是路径名好的,我已经阅读了关于这方面的文档,看起来这正是我需要的。我实施了,但仍然没有工作,所以我又去钓鱼了。我发现我的currentUrl仅返回/I使用currentUrl=window.location.pathname;获取url。有什么想法吗?找到了。我需要使用主机名而不是路径名