Jquery在一个DIV中多次加载

Jquery在一个DIV中多次加载,jquery,html,load,Jquery,Html,Load,这是我的密码: $('#right').load('textes.html #nicolas'); $('#right').load('textes.html #antoine'); 问题是,divantoine的内容覆盖了右边的div nicolas加载的内容div div #right : load div nicolas from file textes.html = ok div #right : load div antoine from file textes.html = ove

这是我的密码:

$('#right').load('textes.html #nicolas');
$('#right').load('textes.html #antoine');
问题是,
div
antoine的内容覆盖了右边的div nicolas加载的内容
div

div #right : load div nicolas from file textes.html = ok
div #right : load div antoine from file textes.html = overwrite content = No!
我想把安托万附加到尼古拉斯身上。这是添加nicolas,然后添加antoine,因此
#right
将是nicolas+antoine

我曾试图将内容放入一个var中,但没有成功

有什么想法吗


除此之外。。。我想在每次加载之间添加一条规则


也许是这样,但这不管用

$('#right').load('textes.shtml #nicolas').append('<hr>').load('textes.shtml #antoine'); return false;
$('#right').load('textes.shtml#nicolas').append('hr>').load('textes.shtml#antoine');返回false;

为什么不在一次调用中同时加载它们:

$('#right').load('textes.html #nicolas,#antoine');
编辑
受正义的启发,我想到了以下几点:

var $page = $('<div />').load('textes.html #nicolas,#antoine');
var $nicolas = $page.find('#nicolas');
var $antoine = $page.find('#antoine');
$('#right')
 .html($nicolas)
 .append('<hr/>')
 .append($antoine);
var$page=$('').load('textes.html#nicolas,#antoine');
var$nicolas=$page.find(“#nicolas”);
var$antoine=$page.find(“#antoine”);
$(“#右”)
.html($nicolas)
.append(“
”) .追加($安托万);
这将只对服务器进行一次(或两次,取决于firefox的感觉)调用。从而节省了带宽。但它也让您在如何插入元素以及插入顺序方面有更多的自由。

var nicolas=$('').load('textes.html#nicolas');
var nicolas = $('<div />').load('textes.html #nicolas');
var antoine = $('<div />').load('textes.html #antoine');
$('#right')
    .append(nicolas.children())
    .append('<hr />')
    .append(antoine.children())
;
var antoine=$('').load('textes.html#antoine'); $(“#右”) .append(nicolas.children()) .append(“
”) .append(antoine.children()) ;

或者,Pim Jager的方式。

请参阅函数

以下是解决方案的完整源代码

我在JSBin.com上主持了一个工作示例:(可编辑通过)


沙箱
$(函数(){
美元(
'http://jsbin.com/oyuho',
功能(响应){
/* 
将响应封装在
查找()而不是筛选器()。同时删除标记。
这就是$.load()的基本功能。
*/  
var responseWrapper=$('').append(response.replace(//g,''))
$(“#内容”)
.append(responseWrapper.find('#nicolas'))
.append(“
”) .append(responseWrapper.find(“#antoine”); } ); });
也许我遗漏了一些东西,但似乎大家都忽略了一个事实,即这是一个ajax调用,您是按过程调用函数,而不是基于成功的ajax响应的回调函数

此外,如果您要做的事情比将一些(X)HTML加载到元素中更复杂,那么您可能应该使用功能更强大的jQuery ajax方法之一(即get()或post()或ajax()

假设您在响应中得到(X)个HTML:

// Only ONE ajax call and very simply parsing... 
$.get('textes.html', {}, function(data) {
    var $response = $('<div />').html(data);
    var $nicolas = $response.find('#nicolas')
    var $antoine = $response.find('#antoine');
    $('#right').append($nicolas).append($antoine);
},'html');
//只有一个ajax调用,并且非常简单地解析。。。
$.get('textes.html',{},函数(数据){
var$response=$('').html(数据);
var$nicolas=$response.find(“#nicolas”)
var$antoine=$response.find(“#antoine”);
$(“#右”).append($nicolas)。append($antoine);
},'html');

就这么简单。

我用KyleFarris非常简单的答案得到了它

我甚至把它简化了一点,效果很好

谢谢大家,这里是最后的代码:

<script>
$.get('textes.shtml', {}, function(data) {
    var $response = $('<div />').html(data);
    $('#right')
    .append($response.find('#nicolas'))
    .append('<hr />')
    .append($response.find('#antoine'));
},'html');
</script>

$.get('textes.shtml',{},函数(数据){
var$response=$('').html(数据);
$(“#右”)
.append($response.find('#nicolas'))
.append(“
”) .append($response.find('#antoine')); },'html');
是否可以在没有额外div标记的情况下进行加载。。。只有纯文本,还有其他的。。。我想在每次加载之间添加一条规则div是一个容器,用于加载任何内容。div是动态创建的,但不会注入到页面中。如果你想做些新奇的事情,我会按照我在这里展示的方式来做。另外,我编辑了这篇文章,展示了如何在两个加载的内容部分之间插入一个
。我喜欢它的工作方式,但遇到了一个错误:拒绝访问受限URI“代码:”1012我认为它与代码没有任何关系,它与加载有关。这是一种比我更好的方式,因为它给了你更多的自由。然而,在最坏的情况下,它会向服务器打四个电话。你真聪明!。。。看起来很简单。。。但是那些奇怪的东西呢$加载('textes.html#a,#b,#c,#d,#e');#right div中的内容顺序不正确。它看起来像a、b、e、d、c或其他任何东西!你自己看,也许我做错了什么,这对我来说真的很复杂,也许当我有时间的时候,我会检查一下,但是KyleFarris解决方案对我来说真的很好,而且它工作得很好,谢谢这些年后我们在这方面的合作。。。刚刚遇到一个情况,在一个ajax表单发布之后,我需要更新多个div。。。此解决方案非常有效,只需稍作调整即可满足我的使用要求。发现此代码非常有用,谢谢!我有一个关于它的问题,假设我在“#nicolas”中有一个div,我需要在这个get call中删除它,我怎么做@KyleFarris
<script>
$.get('textes.shtml', {}, function(data) {
    var $response = $('<div />').html(data);
    $('#right')
    .append($response.find('#nicolas'))
    .append('<hr />')
    .append($response.find('#antoine'));
},'html');
</script>