首页 > 代码库 > 通过JS操作CSS
通过JS操作CSS
动态效果如图所示:
第一种实现方法:
<!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/html; charset=utf-8" /> <title>JavaScript操作CSS:Style</title> <style type="text/css"> li{ font-size: 12px; color: #ffffff; background-image: url(images/bg1.gif); background-repeat: no-repeat; height: 33px; width:104px; text-align: center; line-height:38px; list-style:none; float:left; } </style> <script type="text/javascript"> function changeBg1(currObj){ //style="background-image:url(images/bg2.gif)" //CSS style属性 background-image属性 currObj.style.backgroundImage="url(images/bg2.gif)"; //JavaScript style对象 backgroundImage对象 currObj.style.color="yellow"; currObj.style.fontSize="20px" } function changeBg2(currObj){ currObj.style.backgroundImage="url(images/bg1.gif)"; currObj.style.color="#ffffff"; currObj.style.fontSize="12px" } </script> </head> <body> <ul> <li onm ouseover="changeBg1(this)" onm ouseout="changeBg2(this)" style="background-image:url(images/bg2.gif)">资讯动态</li> <li onm ouseover="changeBg1(this)" onm ouseout="changeBg2(this)">产品世界</li> <li onm ouseover="changeBg1(this)" onm ouseout="changeBg2(this)">市场营销</li> </ul> </body> </html>
第二种实现方法:
<!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/html; charset=utf-8" />
<title>JavaScript操作CSS:Style</title>
<style type="text/css">
li{
font-size: 12px;
color: #ffffff;
background-image: url(images/bg1.gif);
background-repeat: no-repeat;
height: 33px;
width:104px;
text-align: center;
line-height:38px;
list-style:none;
float:left;
}
</style>
<script type="text/javascript">
//匿名函数
//页面加载完调用
window.onload = function(){
var liArr = document.getElementsByTagName("li");
for(var i=0; i<liArr.length; i++){
//动态绑定事件
liArr.item(i).onmouseover = function(){
this.style.backgroundImage = "url(images/bg2.gif)";
this.style.color = "yellow";
this.style.fontSize = "20px";
}
liArr.item(i).onmouseout = function(){
this.style.backgroundImage = "url(images/bg1.gif)";
this.style.color = "#ffffff";
this.style.fontSize = "12px";
}
}
}
</script>
</head>
<body>
<ul>
<li >资讯动态</li>
<li>产品世界</li>
<li>市场营销</li>
</ul>
</body>
</html>
第三种实现方法:(推荐)
<!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/html; charset=utf-8" /> <title>JavaScript操作CSS:Style</title> <style type="text/css"> li{ font-size: 12px; color: #ffffff; background-image: url(images/bg1.gif); background-repeat: no-repeat; height: 33px; width:104px; text-align: center; line-height:38px; list-style:none; float:left; } .over{ background-image:url(images/bg2.gif); color:yellow; font-size:20px; } .out{ background-image:url(images/bg1.gif); color:#ffffff; font-size:12px; } </style> <script type="text/javascript"> //通过js操作css window.onload = function(){ var liArr = document.getElementsByTagName("li"); for(var i=0; i<liArr.length; i++){ //动态绑定事件 liArr.item(i).onmouseover = function(){ this.className = "over"; } liArr.item(i).onmouseout = function(){ this.className = "out"; } } } </script> </head> <body> <ul> <li class="">资讯动态</li> <li>产品世界</li> <li>市场营销</li> </ul> </body> </html>
通过JS操作CSS
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。