Javascript 在输出div中包装四个项,并考虑其余项
我已经尝试了一些方法来让这个工作,但我似乎错过了一些东西 我有一个Javascript 在输出div中包装四个项,并考虑其余项,javascript,jquery,Javascript,Jquery,我已经尝试了一些方法来让这个工作,但我似乎错过了一些东西 我有一个for循环,循环遍历一个对象,并通过将每个对象附加到包装器#人员列表包装器,为每个对象输出一张图片。它目前在自己的.staff项中输出35个图像 for(var i=0;i<staffArray.length;i++){ var img = '<div class="staff-item"><img id="'+staffArray[i].slug+'" src="'+staffArray[i].p
for
循环,循环遍历一个对象,并通过将每个对象附加到包装器#人员列表包装器
,为每个对象输出一张图片。它目前在自己的.staff项中输出35个图像
for(var i=0;i<staffArray.length;i++){
var img = '<div class="staff-item"><img id="'+staffArray[i].slug+'" src="'+staffArray[i].pic_url+'" /></div>';
$('#staff-list-wrapper').append(img);
}
这将创建9行。如何才能在该行中获得每四个项目?我今天遇到了类似的问题。我确信有更优雅的方法来解决这个问题,但这是我最初的解决方案
<?php
$testArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
$numberOfElementsInArray = count($testArray);
$positionInArray = 0; // need to save our position
$howManyForFullSet = 4; // pick how many items you want in each set
$fullSets = floor($numberOfElementsInArray / $howManyForFullSet);
$remaining = $numberOfElementsInArray % $howManyForFullSet;
echo "number of elements in array = " . $numberOfElementsInArray;
echo "\n";
echo "full sets of (" . $howManyForFullSet . ") = " . $fullSets;
echo "\n";
echo "remaining elements = " . $remaining;
echo "\n";
echo "\n";
for ($i = $fullSets; $i > 0; $i--) { // loop for each full set
for ($j = $howManyForFullSet; $j > 0; $j--) { // each element in set
echo($testArray[$positionInArray]);
$positionInArray += 1;
}
echo "\n";
}
if ($remaining !== 0) {
for ($k = $remaining; $k > 0; $k--) { // if we have remainder items display them
echo($testArray[$positionInArray]);
$positionInArray += 1;
}
}
var rowCount = 1;
if(i % 4 === 0 ){
$('#staff-list-wrapper').append('<div class="staff-row" id="staff-row-'+rowCount+'" ></div>');
}
<?php
$testArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
$numberOfElementsInArray = count($testArray);
$positionInArray = 0; // need to save our position
$howManyForFullSet = 4; // pick how many items you want in each set
$fullSets = floor($numberOfElementsInArray / $howManyForFullSet);
$remaining = $numberOfElementsInArray % $howManyForFullSet;
echo "number of elements in array = " . $numberOfElementsInArray;
echo "\n";
echo "full sets of (" . $howManyForFullSet . ") = " . $fullSets;
echo "\n";
echo "remaining elements = " . $remaining;
echo "\n";
echo "\n";
for ($i = $fullSets; $i > 0; $i--) { // loop for each full set
for ($j = $howManyForFullSet; $j > 0; $j--) { // each element in set
echo($testArray[$positionInArray]);
$positionInArray += 1;
}
echo "\n";
}
if ($remaining !== 0) {
for ($k = $remaining; $k > 0; $k--) { // if we have remainder items display them
echo($testArray[$positionInArray]);
$positionInArray += 1;
}
}