JavaScript动态改变样式访问技术
Java  /  管理员 发布于 3年前   147
一:style属性
格式:
HTML元素.style.样式属性="值";
创建菜单:在html标签中创建 或者 在head标签中用数组创建
<html><head> <title>style改变样式</title></head><style type="text/css"> li{ font-size:12px;color:#000000;background-color:#f7f7f7;width:104px;height:33px;line-height:38px;float:left;list-style:none;text-align:center; }</style><body><ul> <li onmouseover="this.style.backgroundColor='#777777'" onmouseout="this.style.backgroundColor='#f7f7f7'">新闻</li> <li onmouseover="this.style.backgroundColor='#777777'" onmouseout="this.style.backgroundColor='#f7f7f7'">视频</li></ul></body></html>
js数组操作样式;
关键代码;
<ul> <li>新闻</li> <li>视频</li> <li>小说</li></ul>
<script type="text/JavaScript"> // document.getElementsByTagName("li"); 获取所有的li标签 var len = document.getElementsByTagName("li"); //为每一个li设置事件和效果 for(var i=0;i < len.length;i++){ len [i].onmouseover = function(){ this.style.background="#777777"; }len [i].onmouseout = function(){ this.style.background="#f7f7f7"; } } </script>
二:ClassName属性
在HTML DOM中 className属性可设置元素或返回元素的class样式;语法
HTML元素.classname="样式名称";
在标签中实现,关键代码;
.out{ background-color:#f7f7f7; } .over{ background-color:#777777; } </style><body><ul> <li onmouseover="this.className='over'" onmouseout="this.className='out'">新闻</li> <li onmouseover="this.className='over'" onmouseout="this.className='out'">视频</li></ul></body>
在js中使用className;
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号