Javascript 每个div类的jQuery查找子类和insertAfter

Javascript 每个div类的jQuery查找子类和insertAfter,javascript,jquery,html,each,children,Javascript,Jquery,Html,Each,Children,我正在编辑我的博客主题,我需要在一篇特定的博客文章中移动(剪切和粘贴)多个特定的div 当前的HTML将是 <div class="blog_post text_post"> <div class="post_container"> <div class="bottom_meta"></div> <div class="post_date"></div> <div

我正在编辑我的博客主题,我需要在一篇特定的博客文章中移动(剪切和粘贴)多个特定的div

当前的HTML将是

<div class="blog_post text_post">
    <div class="post_container">
        <div class="bottom_meta"></div>
        <div class="post_date"></div>
        <div class="moreinfo"></div>
        <!-- I need .bottom_meta here -->
    </div>
</div>
乍一看还行,但当我写另一篇文字文章(通常是图片)时,我的博客就乱七八糟了。 代码现在从两个帖子的.bottom\u meta移动到.moreinfo下

我尝试使用每个函数,但失败了多次

$(".blog_post.text_post").each(function(){     
     $(this).children(".bottom_meta").insertAfter.$(this).children(".moreinfo");
});
我用“find”函数代替“children”尝试了同样的方法,但还是失败了

谁能帮我解决这个问题,或者至少给我指出正确的方向。

应该是:

$(".blog_post.text_post").each(function(){     
    $(this).find(".bottom_meta").insertAfter($(this).find(".moreinfo"));
});

  • .insertAfter.$(this).children(…)
    更改为:
    .insertAfter($(this).children(…)

  • 另外,由于
    .moreinfo
    /
    .bottom\u meta
    元素不是直接子元素,因此需要使用
    .find()
    而不是
    .children()

您还可以将其简化为以下内容:

$(".blog_post.text_post").each(function () {
    $(".bottom_meta", this).insertAfter($(".moreinfo", this));
});

你也可以发布一些HTML吗?这很快:D由于某些原因,第一个解决方案不起作用。但第二个很有魅力。非常感谢。@Morsus是的,我根据您提供的HTML编辑了它。现在两者都应该起作用了。我补充了一个解释。基本上,
.find()
将查找所有子元素,而
.children()
将只查找直接的子元素。第二种解决方案本质上只是
.find()
方法的简写。
$(".blog_post.text_post").each(function () {
    $(".bottom_meta", this).insertAfter($(".moreinfo", this));
});