如何利用JavaScript进行LI列表数据绑定,获取列表里面对应的LI,看了才知道,原来并不很难。
运行效果如下图所示:
具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml"
> <head> <title>JS数据绑定</title> </head> <body> <ul id= "list" > <li><a href= "http://www.wei2008.com/news/news/70585.htm"
target= "_blank" >JavaScript运动减速效果实例分析</a></li> <li><a href= "http://www.wei2008.com/news/news/70584.htm"
target= "_blank" >JavaScript仿静态分页实现方法</a></li> <li><a href= "http://www.wei2008.com/news/news/70583.htm"
target= "_blank" >JavaScript实现选择框按比例拖拉缩放的方法</a></li> <li><a href= "http://www.wei2008.com/news/news/70582.htm"
target= "_blank" >Javascript实现可旋转的圆圈实例代码</a></li> <li><a href= "http://www.wei2008.com/news/news/70581.htm"
target= "_blank" >JavaScript数组各种常见用法实例分析</a></li> </ul> <script type= "text/javascript" > var
list_obj = document.getElementById( "list" ).getElementsByTagName( "li" ); //获取list所有li对象数组 for
( var
i = 0; i <= list_obj.length; i++) { list_obj[i].onmousemove = function () { this .style.backgroundColor = "#cdcdcd" ; } list_obj[i].onmouseout = function () { this .style.backgroundColor = "#FFFFFF" ; } list_obj[i].onclick = new
function (n) { return
function (){alert( "这是第"
+(n+1)+ "条" );} }(i); } </script> </body> </html> |