怎么才能在不同的ul中的li里用js实现隔行变色,如果ul的id为list

JavaScript017

怎么才能在不同的ul中的li里用js实现隔行变色,如果ul的id为list,第1张

var list = document.getElementsByTagName("ul") // 查找所有ul

var len = list.length

for (j = 0 j < len j++) {

    if (list[j].className != 'list') {

        // 如果ul的className不是list

        // 那么执行下一次循环

        continue

    }

    var li = list[j].children // ul下的li

    var liLen = li.length

    for (i = 0 i < liLen i++) {

        if (i % 2 == 0) {

            li[i].className = "list1"

        } else {

            li[i].className = "list2"

        }

    }

}

本文实例为大家分享了jQuery实现表格隔行换色的具体代码,供大家参考,具体内容如下

<!DOCTYPE

html>

<html>

<head>

<meta

charset="UTF-8">

<title>使用JQ完成表格隔行换色</title>

<script

src="js/jquery.min.js"></script>

<script>

$(function()

{

$("tbody

tr:odd").css("background-color","aquamarine")

$("tbody

tr:even").css("background-color","bisque")

var

tb

=

$("tbody

tr")

var

oldColor

for(var

i=0i<tb.lengthi++)

{

//alert(oldColor)

$("tbody

tr")[i].onmouseover

=

function()

{

oldColor

=

this.style.backgroundColor

this.style.backgroundColor

=

"yellow"

}

$("tbody

tr")[i].onmouseout

=

function()

{

this.style.backgroundColor

=

oldColor

}

}

})

</script>

</head>

<body>

<table

id="tbl"

border="1"

border-collapse="collapse"

align="center"

cellspacing="0"

cellpadding="5"

width="400"

height="20">

<thead>

<tr>

<th>编号</th><th>姓名</th><th>年龄</th>

</tr>

</thead>

<tbody>

<tr>

<td>1</td><td>张三</td><td>12</td>

</tr>

<tr>

<td>2</td><td>李四</td><td>22</td>

</tr>

<tr>

<td>3</td><td>王五</td><td>13</td>

</tr>

<tr>

<td>4</td><td>马六</td><td>14</td>

</tr>

<tr>

<td>5</td><td>伍六七</td><td>17</td>

</tr>

<tr>

<td>6</td><td>梅花十三</td><td>17</td>

</tr>

</tbody>

</table>

</body>

</html>

疑问:为什么this.style.backgroundColor

=

oldColor中的this不能用$("tbody

tr")[i]代替??

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:jquery实现表格隔行换色效果jQuery插件实现表格隔行换色且感应鼠标高亮行变色使用jquery

hover事件实现表格的隔行换色功能示例基于Jquery的表格隔行换色,移动换色,点击换色插件