JavaScript动态改变样式访问技术

 

一: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 οnmοuseοver="this.style.backgroundColor='#777777'"  οnmοuseοut="this.style.backgroundColor='#f7f7f7'">新闻</li>
	  <li οnmοuseοver="this.style.backgroundColor='#777777'"  οnmοuseοut="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 οnmοuseοver="this.className='over'" οnmοuseοut="this.className='out'">新闻</li>
	  <li οnmοuseοver="this.className='over'" οnmοuseοut="this.className='out'">视频</li>
	</ul>
</body>

 

 

 

在js中使用className;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值