javascript - 每个 div 类的 jQuery 查找子项和 insertAfter

标签 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 class="moreinfo"></div>
        <!-- I need .bottom_meta here -->
    </div>
</div>

我设法让它像这样工作:

$(".blog_post.text_post .bottom_meta").insertAfter(".blog_post.text_post .moreinfo");

乍一看还不错,但当我写另一篇文字帖子(通常帖子是图片)时,它让我的博客变得一团糟。 现在,代码将两个帖子中的 .bottom_meta 移动到 .moreinfo 下。

我尝试使用每个功能,但都失败了很多次...

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

我用“查找”功能而不是“ child ”尝试了同样的方法,仍然失败。

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

最佳答案

应该是:

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

Example Here

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

  • 此外,由于 .moreinfo/.bottom_meta 元素不是直接子元素,您需要使用 .find() 而不是 .children()

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

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

Example Here

关于javascript - 每个 div 类的 jQuery 查找子项和 insertAfter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28258923/

相关文章:

javascript - JQuery中如何获取父<a>标签元素的文本值

javascript - 从所选选项的模型中获取属性

javascript - 时间轴上的 After Effects 合成开始时间

jquery - IFrame 替代品

jquery - $ 和 $.fn 有什么区别?

html - 如何在滚动条上淡出图像?

javascript - 在 jquery 中调用同名函数?

javascript - JSON 和单选按钮输入?

javascript - 添加了不触发脚本的自定义属性

html - 如何固定表头的位置并仅滚动表体