首页 > 编程语言 > 详细

JavaScript动态改变样式访问技术

时间:2014-11-11 02:28:12      阅读:290      评论:0      收藏:0      [点我收藏+]

?

一: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;

?

?

?

JavaScript动态改变样式访问技术

原文:http://baihe747.iteye.com/blog/2154272

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!