How can I wrap a set of HTML elements within a parent HTML element using JavaScript or jQuery and keep them within each parent element?
P粉204136428
2023-08-17 15:27:06
<p>I'm trying to wrap all <span> tags in one </span></p><div> tag inside another <div> tag using JavaScript or jQuery. I want to find all elements with class name <code>wpb_wrapper</code>, then find all <span> tags in them, and wrap these <span> tag groups inside each <code> in a separate <div> tag within wpb_wrapper</code> so that each group of child <span> tags within <code>wpb_wrapper</code> is still a child element, but wrapped inside a < ;div> tags, rather than wrapping each <span> tag in a separate <div> tag on the page. <p><br /></p>
<p>From this:</p>
<pre class="brush:php;toolbar:false;"><div class="wpb_wrapper">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
<div class="wpb_wrapper">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
<div class="wpb_wrapper">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div></pre>
<p>Changes to this:</p>
<pre class="brush:php;toolbar:false;"><div class="wpb_wrapper">
<div class="">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
</div>
<div class="wpb_wrapper">
<div class="">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
</div>
<div class="wpb_wrapper">
<div class="">
<span class=""></span>
<span class=""></span>
<span class=""></span>
<span class=""></span>
</div>
</div></pre>
<p>I tried this, but it still just grabs all the <span> tags in <code>wpb_wrapper</code> and wraps it in the first <code>wpb_wrapper</code> in a separate </span></p><div> tag<p><br /></p>
<pre class="brush:php;toolbar:false;">jQuery('.wpb_wrapper').each(function(){
jQuery(this).find('span').wrapAll('<div class=""/>');
});</pre>
<p><br /></p>
<pre class="snippet-code-js lang-js prettyprint-override"><code>jQuery('.wpb_wrapper').each(function() {
jQuery(this).find('span').wrapAll('<div class=""/>');
});
console.log(document.body.innerHTML);</code></pre>
<pre class="snippet-code-css lang-css prettyprint-override"><code>.wpb_wrapper { border: 1px solid; }
.wpb_wrapper > div { border: 1px dotted red; }
.wpb_wrapper > div > span { border: 1px dashed blue; }</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/ jquery.min.js"></script>
<div class="wpb_wrapper">
<span class="">0</span>
<span class="">0</span>
<span class="">0</span>
<span class="">0</span>
</div>
<div class="wpb_wrapper">
<span class="">1</span>
<span class="">1</span>
<span class="">1</span>
<span class="">1</span>
</div>
<div class="wpb_wrapper">
<span class="">2</span>
<span class="">2</span>
<span class="">2</span>
<span class="">2</span>
</div></code></pre>
<p><br /></p></div></div></span></div></span></div></span></span></div></div>
Very straightforward, we just append the span element to the newly created div element. However, the problem is that we assume we append it to the end of wgb_wrapper.