【问题标题】:jQuery move multiple DOM elements into other elements using their classes (not IDs)jQuery 使用它们的类(而不是 ID)将多个 DOM 元素移动到其他元素中
【发布时间】:2012-09-28 02:57:32
【问题描述】:

我正在处理包含多篇博客文章的页面,由于当前平台/模板的限制,无法移动 HTML 中的评论计数。

我想将 .reaction-count 元素从每篇文章的一部分移到另一部分。

我已阅读此线程上的解决方案:How to move an element into another element?,但我仍然不确定如何将其转化为我需要的解决方案。

这是我迄今为止尝试过的。它不太有效,因为它将所有 .reaction-count 元素移动到所有 .post-title 元素中,而不仅仅是该特定文章的 .reaction-count。

一次尝试: http://jsfiddle.net/DanEvans/jbgC4

另一个尝试: http://jsfiddle.net/DanEvans/jbgC4/5/

下面是一些模拟这种情况的简化示例 HTML:

<div class="entry-content">
    <h2 class="post-title">
        <a href="#">Post #1</a>
    </h2>

    <p>Lots of other text and elements</p>

    <span class="reaction-count">
        <br><a href="#">Post #1 Comments</a>
    </span>
</div>
<hr>
<div class="entry-content">
    <h2 class="post-title">
        <a href="#">Post #2</a>
    </h2>

    <p>Lots of other text and elements</p>

    <span class="reaction-count">
        <br><a href="#">Post #2 Comments</a>
    </span>
</div>​

我尝试的第一件事之一:

$('.reaction-count').appendTo('.post-title');​

提前感谢您的帮助!

【问题讨论】:

    标签: jquery class dom element move


    【解决方案1】:

    如果您只是想将反应计数移动到相同条目内容的帖子标题中..

    $('.reaction-count').each(function(){
        $(this).parent().find('.post-title').append($(this));
    });  ​  ​
    

    (我的遍历错了)。

    会将反应计数移动到其父条目内容中的帖子标题中。

    Fiddle with your CSS included

    【讨论】:

    • 我最终将 parent() 更改为 parents('.entry-content') 之前在现场网站上使用它,因为有一些其他元素在起作用
    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 2014-09-06
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多