Jquery 如何迭代嵌套UL以将h4从div上方移动到div下方?
我有一个博客主题,它为其博客索引页面生成以下输出。对于每篇文章(每页10篇),它也会迭代相关的评论列表(每篇文章n篇)。在每个注释中,div.commentcontent前面有一个h4元素Jquery 如何迭代嵌套UL以将h4从div上方移动到div下方?,jquery,appendto,detach,Jquery,Appendto,Detach,我有一个博客主题,它为其博客索引页面生成以下输出。对于每篇文章(每页10篇),它也会迭代相关的评论列表(每篇文章n篇)。在每个注释中,div.commentcontent前面有一个h4元素 <ul id="postlist"> <li class="post"> <h2>Post Title</h2> <p>Here is some post content</p> &
<ul id="postlist">
<li class="post">
<h2>Post Title</h2>
<p>Here is some post content</p>
<ul class="commentlist">
<li class="comment">
<h4>Comment Author Name <span class="meta">Date</span></h4>
<div class="commentcontent">Here is the comment text</div>
</li>
</ul>
</li>
</ul>
额外:我也有AJAX处理评论发布,所以当发布新评论时,他们也需要重新定位h4,即使页面没有刷新
额外2:是否可以在注释作者姓名周围环绕span标记
任何帮助都将不胜感激。谢谢
$('#postlist li').each(function() {
$('.comment').each(function() {
var commentMeta = $(this).children('h4').detach();
commentMeta.appendTo($(this))
});
});
编辑:-对于ajax评论发布,您可以创建一个函数,并在ajax方法成功/完成时调用该函数
function swap()
{
$('#postlist li').each(function() {
$('.comment').each(function() {
if($(this).children(':first-child').prop('tagName')=="H4")
{
var commentMeta = $(this).children('h4').detach();
commentMeta.appendTo($(this))
}
});
});
}
$.ajax(
{
url: //,
data: //,
success: function()
{
swap();
}
})
就这样:)谢谢。如果你有任何一个额外的答案,让我知道。干杯。@Suzanne-这不符合您在问题中的要求,它将H4附加到
.commentcontent
元素中,您明确要求H4“跟随DIV”,而不是在DIV内?@Suzanne oops在编辑时忘记了这一点。将appendTo($(this).children('.commentcontent'))
更改为appendTo($(this))
@adeneo您是对的。但是,只要它跟随内容并在注入的评论表单之前登陆,这种方法同样有效。
function swap()
{
$('#postlist li').each(function() {
$('.comment').each(function() {
if($(this).children(':first-child').prop('tagName')=="H4")
{
var commentMeta = $(this).children('h4').detach();
commentMeta.appendTo($(this))
}
});
});
}
$.ajax(
{
url: //,
data: //,
success: function()
{
swap();
}
})