简体   繁体   中英

Wrap around a specific div

I tried Wrap and WrapALl and also nextUntil but its quite tricky this one. I have this html

     <ul class="quick-list quick-glance hideOnSearch show-for-medium-up">
       <li><span class="strong">Price:</span> $198000</li>
       <li><span class="strong">Miles:</span> 349999</li>
       <li><span class="strong">Vehicle Type:</span> Minivans and Vans</li>
     </ul>

and I want it to look like

    <ul class="accordion show-for-small hide-for-medium-up">
      <li>
        <div class="title">TITLE</div>
        <div class="content"> 
         
             <ul class="quick-list quick-glance hideOnSearch show-for-medium-up">
               <li><span class="strong">Price:</span> $198000</li>
               <li><span class="strong">Miles:</span> 349999</li>
               <li><span class="strong">Vehicle Type:</span> Minivans and Vans</li>
             </ul>
        
        </div>
      </li>
    </ul>

I tried this

$('ul.quick-list').wrapAll('<ul class="accordion"><li><div class="title">TITLE</div><div class="content"></div></li></ul>'); //Single page Top accordion

But it places quick-list in the wrong places.

How can I do it with jQuery?

The wrapAll() only supports one inner element, you have two. Solution:

$('ul.quick-list').wrap('<ul class="accordion"><li><div class="content"></div></li></ul>');
$('.content').before('<div class="title">TITLE</div>');

See an example: http://jsfiddle.net/powtac/HzDCD/1/

try this DEMO

$(function(){
$('ul.quick-list').wrap('<ul class="accordion show-for-small hide-for-medium-up">').wrap("<li>").wrap('<div class="content">');

$('.content').before('<div class="title">TITLE</div>');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM