jquery - 如何在一节课后将所有 sibling 包装在一个类(class)中? (jQuery)

标签 jquery html css

我的博客文章有特色图片,其中包含标题、描述和日期等内容详细信息。

<div id="blog-posts-loop">

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <h2 class="blog-home-post-title">This is a title</h2>
        <div class="blog-home-post-excerpt"></div>
        <div class="blog-home-post-date">November 23, 2015</div>

    </div>

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <h2 class="blog-home-post-title">This is a title</h2>
        <div class="blog-home-post-excerpt"></div>
        <div class="blog-home-post-date">November 23, 2015</div>

    </div>

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <h2 class="blog-home-post-title">This is a title</h2>
        <div class="blog-home-post-excerpt"></div>
        <div class="blog-home-post-date">November 23, 2015</div>

    </div>

</div>

期望的输出:我想在每个 class="blog-home-thumb"之后将所有剩余的细节包装为一个带有 class 的 div

<div id="blog-posts-loop">

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <div class="blog-home-content">
            <h2 class="blog-home-post-title">This is a title</h2>
            <div class="blog-home-post-excerpt"></div>
            <div class="blog-home-post-date">November 23, 2015</div>
        </div>

    </div>

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <div class="blog-home-content">
            <h2 class="blog-home-post-title">This is a title</h2>
            <div class="blog-home-post-excerpt"></div>
            <div class="blog-home-post-date">November 23, 2015</div>
        </div>

    </div>

    <div class="blog-home-wrap">
        <div class="blog-home-thumb">
            <a href="#" class="su-post-thumbnail">
            <img width="500" height="300" alt="welcome" class="post-thumbnail" src="src_of_img"></a>
        </div>

        <div class="blog-home-content">
            <h2 class="blog-home-post-title">This is a title</h2>
            <div class="blog-home-post-excerpt"></div>
            <div class="blog-home-post-date">November 23, 2015</div>
        </div>

    </div>



</div>

感谢您的帮助:)

最佳答案

您可以遍历每个 .blog-home-thumb 元素,使用 .nextAll() method, 选择所有以下同级元素然后使用 .wrapAll() method :

Example Here

$('.blog-home-thumb').each(function () {
    $(this).nextAll().wrapAll('<div class="blog-home-content"></div>');
});

关于jquery - 如何在一节课后将所有 sibling 包装在一个类(class)中? (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33875335/

相关文章:

javascript - 悬停时显示复选框 ID、名称和值

javascript - 错误 : Parse error on line 1: in json data

javascript - jquery中添加类后触发事件

php - AJAX 悬停工具提示仅显示表中第一行的返回数据

html - 流畅的 Twitter Bootstrap 布局,左侧和右侧(侧边栏)具有最小宽度的列

javascript - 单击显示时如何始终显示隐藏的 div,在其他 div 之上

javascript - 点击事件触发 child ?

jquery - 如何在 iframe 中获取叠加元素

javascript - 使用 Javascript 在我的网站上格式化数字

css - 我如何在单词之间放置垂直元素符号点?