Insert
for every 5 elements using Javascript

前端 未结 6 558
悲哀的现实
悲哀的现实 2020-12-31 17:49

I have a simple list of images that is being controlled via a CMS (ExpressionEngine). Like this:


      
6条回答
  •  时光取名叫无心
    2020-12-31 18:24

    Here's one way:

    Example: http://jsfiddle.net/T6tu4/

    $('div.wrapper > a').each(function(i) {
        if( i % 5 == 0 ) {
            $(this).nextAll().andSelf().slice(0,5).wrapAll('
    '); } });

    Here's another way:

    Example: http://jsfiddle.net/T6tu4/1/

    var a = $('div.wrapper > a');
    
    for( var i = 0; i < a.length; i+=5 ) {
        a.slice(i, i+5).wrapAll('
    '); }

提交回复
热议问题