javascript - .wrap() 和多元素选择器

标签 javascript jquery html

包装多个元素的最佳方式是什么。我尝试了几种方法但没有成功。

这是标记:

<div>
   <h3>Civil aerospace </h3>
   <p>Powering more than 30 civil aircraft types from small executive jets to the lartest airliners.</p>
   <h4>£47.1bn</h4>
   <p>Order book</p>
   <h4>£4,481m</h4>
   <p>Revenue</p>
</div>

这就是我需要结束的:

<div class="hub">
   <h3>Civil aerospace </h3>
   <p>Powering more than 30 civil aircraft types from small executive jets to the lartest airliners.</p>
    <div class="wrap">
     <h4>£47.1bn</h4>
     <p>Order book</p>
     </div>
    <div class="wrap">
     <h4>£4,481m</h4>
     <p>Revenue</p>
    </div>
</div>

已尝试添加、查找、过滤等

我需要设置迭代吗?

最佳答案

抱歉,不得不出去吃点油炸食品 :)

Scharrels 是正确的,但这里还有一个适用于 jQuery 1.3.2 的可能解决方案:

$(function(){
    $('div').addClass('hub').find('h4').wrap('<div class="wrap"></div>');
    $('.wrap').each(function(){
        var div = $(this);
        div.append(div.next('p').remove());

        while(div.next('p').length != 0)
            div.append(div.next('p').remove());
    });
});

这有点繁琐,但它确实有效,而且它确实满足了多个 <p> 的需求。每个 <h4> 之后的标签.

关于javascript - .wrap() 和多元素选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161306/

相关文章:

javascript - 如何在悬停时突出显示文本

javascript - 如何在 Vue3 中使用 ref 访问父组件中的子组件方法?

jquery - 使用 jquery 的 Google 文本转语音 onclick - 跨浏览器实现

javascript - jQuery UI 日期选择器 : move to the next field after date select

jquery - RadioButton 可拖动只能移动单选按钮圆圈而不移动值为什么?

javascript - 使用javascript在div中显示游戏分数

php - AJAX 加载后没有发生 jQuery 事件?

javascript - Ember JS : Sequential dependent async call to process

python - 如何在 yattag 中添加提取的 html?

javascript - 覆盖 HTML 5 输入类型文件,带有来自 HTML 网页的图像/文本的相机