css 脚本解释

html-css09

css 脚本解释,第1张

//声明函数startList()

startList = function() {

//如果浏览器支持DOM

if (document.all&&document.getElementById) {

//将id为nav的元素赋给变量navRoot,(貌似这个navRoot是全局变量,不然要用var声明)

navRoot = document.getElementById("nav")

//定义一个循环,循环变量i的上限是id为nav的元素的字节点数量,此时navRoot的子节点的集合是数组,i自加1

for (i=0i<navRoot.childNodes.lengthi++) {

//将数组navRoot.childNodes下标索引为i的元素赋给变量node

node = navRoot.childNodes[i]

//如果node的标签的是LI

if (node.nodeName=="LI") {

//当鼠标移动到这个LI标签上时触发以下函数

node.onmouseover=function() {

//这句相当于this.className = this.className + " over",className是其class属性对应的css类名

this.className+=" over"

}

//当鼠标离开该元素时出发以下函数

node.onmouseout=function() {

//此时元素的class属性中的" over"被""替换,""表示空

this.className=this.className.replace(" over", "")

}

}

}

}

}

//当页面读取时,触发startList函数

window.onload=startList

用div+css的ul、li结合script脚本实现下拉列表菜单,全部代码如下,复制在DW中预览即可看到效果,细节样式可以自行修改:

<!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>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>DIV+CSS实现下拉列表菜单</title>

<style type="text/css">

<!--

ul {

margin: 0px

padding: 0px

list-style-type: none

}

li{

float:left

width:160px

margin-left:1px

}

ul li a{

display:block

font-size:12px

border:#CCCCCC 1px solid

padding:3px

text-decoration:none

color:#777777

margin-top:1px

text-align:center

}

ul li a:hover{

background-color:#0099CC

color:#FFFFFF

}

li ul{

display:none

top:20px

margin-top:1px

}

li:hover ul,li.over ul{

display:block

}

-->

</style>

</head>

<script type="text/javascript"><!--//--><![cdata]//><!--

startlist = function() {

if (document.all&&document.getElementByIdx) {

navRoot = document.getElementByIdx("nav")

for (i=0i<navRoot.childNodes.lengthi++) {

node = navRoot.childNodes[i]

if (node.nodeName=="li") {

node.onmouseover=function() {

this.className+=" over"

}

node.onmouseout=function() {

this.className=this.className.replace(" over","")

}

}

}

}

}

window.onload=startlist

//--><!]]></script>

</script>

<body>

<ul >

<li><a href="">文章</a>

<ul>

<li><a href="">CSS 教程</a></li>

<li><a href="">DOM 教程</a></li>

<li><a href="">XML 教程</a></li>

<li><a href="">Flash 教程</a></li>

</ul>

</li>

<li><a href="">参考</a>

<ul>

<li><a href="">XHTML</a></li>

<li><a href="">XML</a></li>

<li><a href="">CSS</a></li>

</ul>

</li>

<li><a href="">Blog</a>

<ul>

<li><a href="">全部</a></li>

<li><a href="">网页技术</a></li>

<li><a href="">UI 技术</a></li>

<li><a href="">Flash 技术</a></li>

</ul>

</li>

<li><a href="">摇滚</a>

<ul>

<li><a href="">纯音乐</a></li>

<li><a href="">古典金曲</a></li>

<li><a href="">UI 技术</a></li>

<li><a href="">Flash 技术</a></li>

</ul>

</li>

</ul>

</body>

</html>