jQuery用另一个替换一个Div

jQuery用另一个替换一个Div

问题描述:

我需要用另一个替换一个div,也需要另一个容器被删除。请参阅我的代码:jQuery用另一个替换一个Div

<a id="load-more" href="#"> 
<div class="text">Load more</div> 
<div id="infscr-loading"> 
<img alt="Loading..." src="../loading_small.gif" style="display: none; "> 
<div">No more posts to load</div> 
</div> 
</a> 

我需要这样的:

<span class="text">Load more</span> 

这种替换:

<div">No more posts to load</div> 

所以,我的结果应该是:

<a id="load-more" href="#"> 
<div">No more posts to load</div> 
<div id="infscr-loading"> 
<img alt="Loading..." src="../loading_small.gif" style="display: none; "> 
</div> 
</a> 

这是可能做什么?

+0

是的,这是可能的。你有什么尝试? – iambriansreed 2012-07-19 19:55:42

你尝试:

$('.text').replaceWith(...); 

文档在http://api.jquery.com/replaceWith/

+0

完美,谢谢! – Anton 2012-07-19 20:10:13

您的标记已在<div">标签错误,可能是你不小心删除的类名或ID。我会假设更换div有id属性。

要改变<div class="text">位的,你需要做的内容:

$('div.text').replaceWith($('#replacement')); 

这既消除了text股利和移动#replacement div来的text div的位置。

$('div.text').replaceWith($('#replacement').html());