css3有选择器可以直接筛选奇偶,例如nth-of-type(),但是该方法并不支持ie.所以我几天讲一个用jquery来兼容的。
首相你要引入jquery.js
然后在页面上写上:
<script type="text/javascript">
$(function() {
$("li:nth-child(2n)").addClass("evencss"); //偶数
$("li:nth-child(2n+1)").addClass("oddcss"); //奇数
});
</script><style>
li.oddcss{ background:red; }
li.evencss{ background:#fff; }
</style>
本文介绍了一种使用jQuery实现CSS3中nth-of-type()选择器功能的方法,以兼容不支持此选择器的老版本浏览器如IE。通过为页面上的元素(如<li>)添加特定的类(奇数或偶数),可以轻松地改变它们的样式。
9927

被折叠的 条评论
为什么被折叠?



