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
P粉204136428 2023-08-17 15:27:06
0
1
449
<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>
P粉204136428
P粉204136428

reply all(1)
P粉659518294

  const wrapper = document.querySelector('.wpb_wrapper');
  const spans = wrapper.querySelectorAll('span');

  const customDiv = document.createElement('div');
  customDiv.classList.add('custom-div');

  spans.forEach(span => {
    customDiv.appendChild(span);
  });

  wrapper.appendChild(customDiv);
<div class="wpb_wrapper">
  <h2><img decoding="async" class="alignnone size-full wp-image-9244" src="icon-briefcase.svg" alt="" width="24" height="24">Functional Experience</h2>
  <p></p>
  <span class=""></span>
  <span class=""></span>
  <span class=""></span>
  <span class=""></span>
</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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template