JavaScript DOM操作之:CSS属性操作基础

CSS属性操作:
	指的是通过JavaScript来操作一个元素的CSS样式
	获取CSS属性值
		getComputedStyle(obj).attr / getComputedStyle(obj)['attr']  两种写法一样
		attr表示css属性名,但必须写成驼峰型,如font-size应该写成fontSize,因为'-'不是有效的js标识符
		
	设置CSS属性值
		style对象
			在元素的style属性中添加样式,这种方式设置的是行内样式
			obj.style.attr='值'
			
		cssText属性
			可以同时设置多个CSS属性,这也是在元素的style属性中添加的
			obj.style.cssText='值'
			如 oDiv.style.cssText='width:100px;height:100px;border:1px solid gray'
			在实际开发中很少使用这种方式设置多个css属性
				而是采用操作html属性的方式给元素加上一个class属性,从而整体地给元素添加属性
		无法使用obj.style.attr的方式来获取内部样式或者外部样式,这种方式只能获取行内样式
		所以获取css的属性值要使用getComputedStyle(obj).attr
			

01获取元素CSS样式属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box{
				width: 6.25rem;
				height: 6.25rem;
				background-color: #2F4F4F;
			}
		</style>
		<script type="text/javascript">
			window.onload=function(){
				var oBtn=document.getElementById('btn');
				var oBox=document.getElementById('box');
				oBtn.onclick=function(){
					alert(getComputedStyle(oBox).backgroundColor);
				}
			}
		</script>
	</head>
	<body>
		<div id="box">
			
			
		</div>
		<input type="button" name="" id="btn" value="查看" />
	</body>
</html>

02style对象设置css行内样式

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box{
				width: 6.25rem;
				height: 6.25rem;
				background-color: aqua;
			}
		</style>
		<script type="text/javascript">
			window.onload=function(){
				var oBtn=document.getElementById('btn');
				var oBox=document.getElementById('box');
				oBtn.onclick=function(){
					oBox.style['backgroundColor']='lightskyblue';
					oBox.style.border='2px solid green';
				}
			}
		</script>
	</head>
	<body>
		<div id="box">
			
		</div>
		<input type="button" id="btn"  value="修改"/>
	</body>
</html>

03复合属性操作

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box{
				width: 6.25rem;
				height: 6.25rem;
				background-color: #2F4F4F;	
			}
			
		</style>
		<script type="text/javascript">
			window.onload=function(){
				var oBtn=document.getElementById('btn');
				var oBox=document.getElementById('box');
				oBtn.onclick=function(){
					//获取两个文本框的值
					var attr = document.getElementById('attr').value;
					var val =document.getElementById('val').value;
					oBox.style[attr]=val;
				}
				
				
			}
		</script>
	</head>
	<body>
		属性:<input type="text" name="" id="attr" value="" /><br>
		取值:<input type="text" name="" id="val" value="" /><br>
		<input type="button" id="btn" value="设置"/>
		<div id="box">
			
		</div>
	</body>
</html>

04cssText属性设置举例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#box{
				width: 6.25rem;
				height: 6.25rem;
				background-color: #00FFFF;
			}
		</style>
		<script type="text/javascript">
			window.onload=function(){
				var oBtn=document.getElementById('btn');
				var oBox=document.getElementById('box');
				oBtn.onclick=function(){
					// 获取文本框的值
					var txt = document.getElementById('txt').value;
					oBox.style.cssText=txt;
				};
			}
		</script>
	</head>
	<body>
		<input type="text" name="" id="txt" value="" /><br>
		<input type="button" name="" id="btn" value="设置" />
		<div id="box">
			
		</div>
	</body>
</html>

05使用html属性操作来修改多个css属性

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.oldbox{
				width: 100px;
				height:100px;
				background-color: antiquewhite;
			}
			.newbox{
				width: 200px;
				height:200px;
				background-color: #2F4F4F;
			}
			
		</style>
		<script type="text/javascript">
			window.onload=function(){
				var oBtn = document.getElementById('btn');
				var oBox=document.getElementById('box');
				oBtn.onclick=function(){
					oBox.className='newbox';
				}
			}
		</script>
	</head>
	<body>
		<input type="button" name="" id="btn" value="切换" />
		<div id="box" class="oldbox">
		
		</div>
	</body>
</html>

生命不息,流汗不止.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值