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 ele开发者_StackOverflow中文版ments 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/
精彩评论