What i have here is this:
<ul class="bwp-rc-ulist">
<li class="recent-comment">Item 1</li>
<li class="recent-comment">Item 2</li>
<li class="recent-comment">Item 3</li>
<li class="recent-comment">Item 4</li>
<li class="recent-comment">Item 5</li>
<li class="recent-comment">Item 6</li>
<li class="recent-comment">Item 7</li>
<li class="recent-comment">Item 8</li>
<li class="recent-comment">Item 9</li>
<li class="recent-comment">Item 10</li>
</ul>
I want to add the following CSS styles:
li.list1, li.list6 { background-color: red; }
li.list2, li.list7 { background-color: blue; }
li.list3, li.list8 { background-color: black; }
li.list4, li.list9 { background-color: yellow; }
li.list5, li.list10 { background-color: gray; }
unfortunately, i cannot do that because the UL & LI is dynamically created by a wordpress plugin (Better Wordpress Comment & Except Plugin).
I've already read some nth-child thingy but it doesn't solve my case. maybe if someone can show me what the correct scripts?
I'm also looking for a script that works in IE7-9, chrome and ff.
Thank you very much.
CSS solution
ul.bwp-rc-ulist li:nth-child(1),
ul.bwp-rc-ulist li:nth-child(6) {
background-color: red;
}
ul.bwp-rc-ulist li:nth-child(2),
ul.bwp-rc-ulist li:nth-child(7) {
background-color: blue;
}
ul.bwp-rc-ulist li:nth-child(3),
ul.bwp-rc-ulist li:nth-child(8) {
background-color: black;
}
ul.bwp-rc-ulist li:nth-child(4),
ul.bwp-rc-ulist li:nth-child(9) {
background-color: yellow;
}
ul.bwp-rc-ulist li:nth-child(5),
ul.bwp-rc-ulist li:nth-child(10) {
background-color: gray;
}
or if you prefer it a bit more compact:
ul.bwp-rc-ulist li:nth-child(5n+1) {
background-color: red;
}
ul.bwp-rc-ulist li:nth-child(5n+2) {
background-color: blue;
}
ul.bwp-rc-ulist li:nth-child(5n+3) {
background-color: black;
}
ul.bwp-rc-ulist li:nth-child(5n+4) {
background-color: yellow;
}
ul.bwp-rc-ulist li:nth-child(5n+5) {
background-color: gray;
}
jQuery solution (with your CSS)
$('ul.bwp-rc-ulist li.recent-comment').each(function(index) {
$(this).addClass("list" + (index + 1));
});
HTML:
<ul id="ulEle" class="bwp-rc-ulist">
<li class="recent-comment">Item 1</li>
<li class="recent-comment">Item 2</li>
...
</ul>
OR:
<div id="ulEle">
<ul class="bwp-rc-ulist">
<li class="recent-comment">Item 1</li>
<li class="recent-comment">Item 2</li>
...
</ul>
</div>
JS:
<script type="text/javascript">
var ulGet=document.getElementById("ulEle");
var liList=ulGet.getElementsByTagName("li");
for(var i=0;i<liList.length;i++)
{
liList[i].style.className="list"+(parseInt(i)+1);
}
</script>
try this css
li:nth-child(6n+1) {
color: red;
}
li:nth-child(6n+2) {
color: blue;
}
and so on
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.