获取元素类型 (h1's) 之间的所有元素并包装在 div 中

发布于 2025-01-20 14:23:54 字数 1094 浏览 2 评论 0原文

我有以下结构:

<div class="wrapper">
    <h1>Header 1</h1>
    <h2>subtitle 1</h2>
    <p>aaa</p>
    <p>bbb</p>
    <p>ccc</p>
    <h2>subtitle2</h2>
    <p>ddd</p>

    <h1>Header 2</h1>
    <h2>subtitle 3</h2>
    <p>eee</p>
    <p>fff</p>

</div>

我想选择每个H1和DIV中的所有H1和P元素,因此我最终得到了:

<div class="wrapper">
    <div>
        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>
    </div>

    <div>
        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
    </div>
</div>

我尝试了以下各种事情,但是没有一个完美工作:

$( "h1" ).prevUntil( "h1" ).wrapAll( "<div></div>" );

I have the below structure:

<div class="wrapper">
    <h1>Header 1</h1>
    <h2>subtitle 1</h2>
    <p>aaa</p>
    <p>bbb</p>
    <p>ccc</p>
    <h2>subtitle2</h2>
    <p>ddd</p>

    <h1>Header 2</h1>
    <h2>subtitle 3</h2>
    <p>eee</p>
    <p>fff</p>

</div>

I want to select all the h1 and p elements between each h1 and wrap in a div, so I end up with:

<div class="wrapper">
    <div>
        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>
    </div>

    <div>
        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
    </div>
</div>

I've tried various things like the below, but none work perfectly:

$( "h1" ).prevUntil( "h1" ).wrapAll( "<div></div>" );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2025-01-27 14:23:54
  1. 您应该使用 .each jQuery API 来完成。
$("h1").each(function(){})
  1. .prevUntil 将反转 Dom 节点遍历,请使用 .nextUntil 代替。

  2. 为了匹配最终输出,脚本如下:

<script>
    $("h1").each(function() {
        $(this).nextUntil( "h1" ).wrapAll( "<div></div>" );
        // uncomment the two lines to move the h1 inside the wrapped div
        // const el = $(this).next();
        // $(this).prependTo(el);
    });
</script>
  1. You shall use .each jQuery API to accomplish.
$("h1").each(function(){})
  1. .prevUntil will inverse the Dom node traversing, use .nextUntil instead.

  2. as to match the end-up output, the script is as follows:

<script>
    $("h1").each(function() {
        $(this).nextUntil( "h1" ).wrapAll( "<div></div>" );
        // uncomment the two lines to move the h1 inside the wrapped div
        // const el = $(this).next();
        // $(this).prependTo(el);
    });
</script>
掩于岁月 2025-01-27 14:23:54

我想出了这个解决方案,它获取所有子级,然后如果它是 h1 标签,它将创建一个新的 div 并将 H1 和其他子级移动到其中

    var newDiv;
    $(document).ready(function(){
        $(".wrapper").children().each(function(){
           if($(this).is("h1")){
              $(this).before("<div></div>");
              newDiv =  $(this).prev();
              newDiv.append($(this));
           }else{
            newDiv.append($(this));
           }
        });
       
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">

        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>

        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
 
</div>

I came up with this solution where it gets all the children and then if its a h1 tag it will create a new div and move H1 and other children into it

    var newDiv;
    $(document).ready(function(){
        $(".wrapper").children().each(function(){
           if($(this).is("h1")){
              $(this).before("<div></div>");
              newDiv =  $(this).prev();
              newDiv.append($(this));
           }else{
            newDiv.append($(this));
           }
        });
       
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">

        <h1>Header 1</h1>
        <h2>subtitle 1</h2>
        <p>aaa</p>
        <p>bbb</p>
        <p>ccc</p>
        <h2>subtitle2</h2>
        <p>ddd</p>

        <h1>Header 2</h1>
        <h2>subtitle 3</h2>
        <p>eee</p>
        <p>fff</p>
 
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文