How to wrap (to group) a list of elements into another element with jquery?

后端 未结 3 1174
南旧
南旧 2020-12-10 17:53

I have the following structure.

  • 相关标签:
    3条回答
    • 2020-12-10 18:35
      var i, num = 3, $ul = $('#ul'), $li = $('#ul > li');
      for (i=0;i<$li.length;i+=num) {
          $li.slice(i,i+num).wrapAll('<ul />');
      }
      $ul.find('> ul').unwrap();
      

      http://jsfiddle.net/Q2bYz/

      0 讨论(0)
    • 2020-12-10 18:52

      You can do it with wrapAll:

      var a = $('li');
      do $(a.slice(0,3)).wrapAll('<ul />');   
      while((a = a.slice(3)).length>0)
      

      example: http://jsfiddle.net/niklasvh/mZr4h/

      0 讨论(0)
    • 2020-12-10 18:54

      .wrap() api is what you are looking for

      0 讨论(0)
    提交回复
    热议问题