Ajax 追加加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax append load
提问by menardmam
it must be jquery
它必须是 jquery
I have file text.html with 6 div in (a,b,c,d,e,f)
我在 (a,b,c,d,e,f) 中有 6 个 div 的文件 text.html
In another file i have a div, i like it to populate the content of a+b+c+d+e+f into that single div
在另一个文件中,我有一个 div,我喜欢将 a+b+c+d+e+f 的内容填充到该单个 div 中
I have try .load = but b remplace a i have try append, but i need a temp var
我已经尝试过 .load = 但是 b remplace a 我已经尝试追加,但我需要一个临时变量
so now i am stuck
所以现在我被困住了
That code get the content from the file textes.html ... div #a and put the content into div #right, but the second libe REMPLACE the content of right a with right b
该代码从文件 textes.html ... div #a 中获取内容并将内容放入 div #right,但第二个 libe 将 right a 的内容替换为 right b
I like to append the content a + b NOT a over b
我喜欢将内容 a + b 附加到 b 上,而不是 a
$(document).ready(function(){
var temp = load('textes.html #nicolas');
$('#right').append(temp);
var temp = load('textes.html #antoine');
$('#right').append(temp);
.
.
.
.
return false;
});
that code is the idea behind what should work, but i cannot make a ajax .load() to load content into a variable to append the content to the div...
该代码是什么应该工作背后的想法,但我不能使 ajax .load() 将内容加载到变量中以将内容附加到 div ...
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url: "textes.html",
cache: false,
success: function(html){
$("#right").append(html);
}
});
});
</script>
That code load the WHOLE html file, i like to get only some selected DIV #
该代码加载整个 html 文件,我喜欢只获取一些选定的 DIV #
回答by Chad Grant
$(document).ready(function(){
$.get("textes.html",function(data){
$("#right").append($("#nicolas",data)).end().append($("#antoine",data));
},'html');
});
回答by Jibou
I had a similar issue just now and I think I figured out a way to do what we want using the .load() function. It's not pretty but never mind ;)
我刚才遇到了类似的问题,我想我想出了一种使用 .load() 函数来做我们想做的事情的方法。它不漂亮,但没关系;)
First off, I added a "TempDiv" to my html with a "visibility:hidden" style.
首先,我用“visibility:hidden”样式在我的html中添加了一个“TempDiv”。
<div id="TempDiv" style="visibility:hidden"></div>
Then you run the jQuery :
然后你运行 jQuery :
$(document).ready(function(){
$('#TempDiv').load('textes.html #nicolas', function(){
$('#right').append($('#TempDiv').html());
});
});
I'm not sure it's the best way !
我不确定这是最好的方法!
PS : That is my first stackoverflow post ;)
PS:这是我的第一个stackoverflow帖子;)
回答by AmusableLemur
try,
尝试,
$.get('url.php', function(data) {
$("#right").append(data);
});
回答by Ko9
This sounds like jQuery? Please state what framework you are using since I can't really see any mention of it. Anyway, append should work. Just do something like:
这听起来像jQuery?请说明您使用的是什么框架,因为我真的看不到任何提及它。无论如何, append 应该可以工作。只需执行以下操作:
mydiv.append(a.text());
mydiv.append(b.text());
mydiv.append(c.text());
mydiv.append(d.text());
mydiv.append(e.text());
mydiv.append(f.text());
They should all be appended into mydiv. NOTE: if you also want the html, use the .html() function instead of .text().
它们都应该附加到 mydiv 中。注意:如果您还需要 html,请使用 .html() 函数而不是 .text()。
回答by Jonathan Parker
JQuery ( http://jquery.com/) is a good javascript library that you can use to do an AJAX request to get the other file. See this question for more: Use jQuery to replace XMLHttpRequest
JQuery ( http://jquery.com/) 是一个很好的 javascript 库,您可以使用它来执行 AJAX 请求以获取其他文件。有关更多信息,请参阅此问题:使用 jQuery 替换 XMLHttpRequest

