JavaScript学习笔记20

DOM操作CSS
1.第一种方式

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>DOM操作CSS</title>
	<style style="text/css">
		li{
			font-size:2px;
			color:#ffffff;
			background-color:gray;
			border-radius:5px;
			height:33px;
			width:100px;
			text-align:center;
			line-height:38px;
			list-style:none;
			float:left;
			margin-right:10px;
		}
	</style>
	<script type="text/javascript">
		function changeA(obj){
			obj.style.color="yellow";
			obj.style.fontSize="16px";
			obj.style.backgroundColor="red";
			
		}
		function changeB(obj){
			obj.style.color="white";
			obj.style.fontSize="12px";
			obj.style.backgroundColor="gray";
		}
	</script>
</head>
<body>
	<ul>
		<li onmouseover="changeA(this)" onmouseout="changeB(this)">前端</li>
		<li onmouseover="changeA(this)" onmouseout="changeB(this)">后端</li>
		<li onmouseover="changeA(this)" onmouseout="changeB(this)" >数据库</li>
	</ul>
</body>
</html>

直接通过在按钮上添加事件,调用函数,实现效果。
注意函数参数问题,如调用时:changeA(this), 定义时:changeA(obj)

2.第二种方式

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>DOM操作CSS</title>
	<style style="text/css">
		li{
			font-size:2px;
			color:#ffffff;
			background-color:gray;
			border-radius:5px;
			height:33px;
			width:100px;
			text-align:center;
			line-height:38px;
			list-style:none;
			float:left;
			margin-right:10px;
		}
	</style>
	<script type="text/javascript">
		//页面加载完后,给所有li动态绑定over和out事件
			window.onload=function(){
			//获取所有li
			var liArr=document.getElementsByTagName("li");
			for(var i=0;i<liArr.length;i++){
				liArr[i].onmouseover=function(){
					this.style.color="yellow";
					this.style.fontSize="16px";
					this.style.backgroundColor="red";
				}
				liArr[i].onmouseout=function(){
					this.style.color="white";
					this.style.fontSize="12px";
					this.style.backgroundColor="gray";
				}
			}
		}
	</script>
</head>
<body>
	<ul>
		<li>前端</li>
		<li>后端</li>
		<li>数据库</li>
	</ul>
</body>
</html>

通过js技术动态绑定事件,这样不用每个标签都写一次。

第三种方式

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>DOM操作CSS</title>
	<style style="text/css">
		li{
			font-size:2px;
			color:#ffffff;
			background-color:gray;
			border-radius:5px;
			height:33px;
			width:100px;
			text-align:center;
			line-height:38px;
			list-style:none;
			float:left;
			margin-right:10px;
		}
		.over{
			color:yellow;
			font-size:16px;
			background-color:red;
		}
		.out{
			color:white;
			font-size:12px;
			background-color:gray;
		}
	</style>
	<script type="text/javascript">
		//页面加载完后,给所有li动态绑定over和out事件
			window.onload=function(){
			//获取所有li
			var liArr=document.getElementsByTagName("li");
			for(var i=0;i<liArr.length;i++){
				liArr[i].onmouseover=function(){
					//this.class="over";// 不是class 而是className
					this.className="over";
				}
				liArr[i].onmouseout=function(){
					this.className="out";
				}
			}
		}
	</script>
</head>
<body>
	<ul>
		<li >前端</li>
		<li>后端</li>
		<li>数据库</li>
	</ul>
</body>
</html>

不仅动态绑定事件,定义样式,改变样式都采用类定义的方法,这样可以增加代码的重用性。

注意点:
js中的样式定义:

					//对象调用时
					this.style.color="yellow";
					this.style.fontSize="16px";
					this.style.backgroundColor="red";

					//在标签中设置时
					<div style="height:50px;width:50px;position:fixed;">

css中的样式定义:

			font-size:2px;
			color:#ffffff;
			background-color:gray;
			border-radius:5px;
			height:33px;
			width:100px;
			text-align:center;
			line-height:38px;
			list-style:none;
			float:left;
			margin-right:10px;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值