简体   繁体   中英

jQuery 5 elements create a new one the wrap the next 5 in

I have a central wrap and within that wrap I want to append a wrap which contains 5 items in it, but for every 5th time through the loop I want to add another wrapper for 5 more elements then continue down the line..

My Issue is I'm not exactly sure how to catch the newly added container for every 5th set of items.. this is my current approach that I am stuck in..

var workSpaceItem = '<div class="ztsWorkSpaceItem" style="width:114px;height:33px;border:#000 solid 1px;color:#FFF;float:left;"></div>'
$.each(workSpaceJSON.workSpace, function(e)
{
    if(e %5 === 0){$('#ztsWorkspaceBarCenter').append('<div class="ztsWorkSpaceItem"></div>');alert('five: '+e);}
});

You may store it in a variable(called "wrapper" in the example):

    var workSpaceItem = '<div class="ztsWorkSpaceItem" style="width:114px;border:#000 solid 1px;float:left;"></div>'
    var wrapper;

    $.each(workSpaceJSON.workSpace, function(e)
    {
      if(e %5 === 0){wrapper=$(workSpaceItem).appendTo('#ztsWorkspaceBarCenter');}
      wrapper.append('<p>'+e+'</p>');
    });


Demo: http://jsfiddle.net/doktormolle/kF8tB/

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