I have a div with several boxes within while loop. I want to give the two alternative div background color. For first 3 boxes i have to give the first color. Then for next 3 boxes i'v to give next color, then again for the next 3 boxes i'v to give the first color and so on.
Clearly i want to give the two colors alternatively for 3 boxes alternatively from first. Can anyone help me for this.
var count=0;
$("div").each(function (i) {
count++;
if (count%3) {
this.style.color = "blue";
} else {
this.style.color = "black";
}
});
var count=0;
if(count%2==0) {
echo"<div style="background:white;">";
}
else {
echo"<div style="background:pink;">";
}
echo"</div>";
The desired result is:
<div class="white">...
<div class="white">...
<div class="white">...
<div class="blue">...
<div class="blue">...
<div class="blue">...
<div class="white">...
Which mean your code should be:
for ($count = 0 ; $count < 20; $count++) {
$color = ($count / 3) % 2 ? "blue" : "white";
echo "<div class='$color'>", "SOMETHING", "</div>";
}
All you're doing is grouping "dividing by 3" then determining if the group is even or odd.
Note: You could easily use a style="background-color:$color"
depending on your needs and CSS info.
I don't think he likes this is JS as his tag is PHP
is this what you are looking for;
<div>
<?php
$numberofResults = '5';
for ($i=1; $i<=$numberofResults; $i++){
if($i % 2 == 0) $style='style="background-color:black"';
else $style='style="background-color:white"';
echo '<box'.$i.' ' . $style . '>';
echo '</box'.$i.'>';
echo "\n";
}
?>
</div>
this will yield;
<div>
<box1 style="background-color:white"></box1>
<box2 style="background-color:black"></box2>
<box3 style="background-color:white"></box3>
<box4 style="background-color:black"></box4>
<box5 style="background-color:white"></box5>
</div>
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.