jquery在DIV中多次加载 [英] Jquery Multiple load in a DIV

查看:99
本文介绍了jquery在DIV中多次加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

  $('#right')。load('textes.html #nicolas'); 
$('#right')。load('textes.html #antoine');

问题在于div antoine的内容覆盖了正确的div, nicolas



div #right:从文件中加载div nicolas textes.html = ok
div #right:从文件加载div antoine textes.html =覆盖content = No!



我喜欢将antoine添加到nicolas
我喜欢添加nicolas,然后添加antone,因此#right将是nicolas + antoine



我试图将内容转换为var ... nope



有什么想法?




最重要的是...我想添加一条规则< hr> BETWEEN每次加载




也许类似这样的事情可以完成......它不起作用!

$ b ('< hr>')。load('); $'$'$ $ $ $ $ $ $''$'$' textes.shtml #antoine');返回false;


解决方案

也许我错过了一些东西,但它看起来像是你所有人都错过了这样一个事实,即这是一个ajax调用,而且你正在程序上调用函数,而不是基于成功的ajax响应的回调函数。



另外,如果你(X)HTML加载到元素中更复杂,您应该使用其中一种更强大的jQuery ajax方法(即get()或post()或ajax())。
$ b

假设您将在响应中获得(X)HTML:

  //只有一个ajax调用和非常简单的解析... 
$ .get('textes.html',{},function(data){
var $ response = $('< div />') .html(data);
var $ nicolas = $ response.find('#nicolas')
var $ antoine = $ response.find('#antoine');
$(' append($ nicolas).append($ antoine);
},'html');

真的很简单。


Here is my code

$('#right').load('textes.html #nicolas');
$('#right').load('textes.html #antoine');

the problem is that the content of the div antoine overwrite in the right div the content load by the div nicolas

div #right : load div nicolas from file textes.html = ok div #right : load div antoine from file textes.html = overwrite content = No!

I like to append antoine to nicolas I like to add nicolas then add antone so the #right will be nicolas + antoine

I have try to get the content into a var... nope

any idea ?


on top of that... i will like to add a rule <hr> BETWEEN each load


Maybe something like that can be done... it don't work !

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

解决方案

Maybe I'm missing something but it seems like you all have been missing the fact that this is an ajax call and you are calling functions procedurally and not as a callback function based on a successful ajax response.

Besides, if you are doing anything more complex than loading some (X)HTML into an element, you should probably use one of the more powerful jQuery ajax methods (i.e., get() or post() or ajax()).

Assuming you'll get (X)HTML in the response:

// 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');

It's really as simple as that.

这篇关于jquery在DIV中多次加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆