javascript不定参数&&兼容性样式设置与获取

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>不定参数/可变参数</title>
		<style type="text/css">
			#div1{
				width: 100px;
				height: 100px;
				background-color:#FF0000;
			}
		</style>
	</head>
	<script type="text/javascript">
		function add(){//可以不定参数
			//alert(arguments.length);//输出参数的个数
			var sum=0;
			var i=0;
			for(i=0;i<arguments.length;i++){//加法计算
				sum+=arguments[i];
			}
			return sum;
		}
		function show(){
			//alert(arguments.length);//输出参数的个数
			//设置层的颜色
				if(arguments[0].currentStyle){//IE
					alert(arguments[0].currentStyle["background-color"]);
				}else{//Firefox,chrome
					alert(window.getComputedStyle(arguments[0],false).getPropertyValue("background-color"));
					//window.getComputedStyle(arguments[0],false)["background-color"];//firefox不支持
				}
			
		}

		//获取属性,或者设置属性,
		function objectStyle(obj,attr,value){
			if (arguments.length==2)//当参数为obj,attr为获取属性值
			{
				if (attr.currentStyle) {//IE
					return obj.currentStyle[attr];
				}else{//others
					return window.getComputedStyle(obj,false).getPropertyValue(attr);
				}
			}else if(arguments.length==3){//当参数为obj,attr,value为设置属性值
				obj.style[attr]=value;
			}
		}

	

		window.οnlοad=function(){
			//alert(add(1,2,3,4,5,6,7,8,9,10));//调用不定参数的加法运算
			var oBtnGetColor=document.getElementById("btnGetColor");//获取层颜色
			var oBtnSetColor=document.getElementById("btnSetColor");//设置层的颜色
			var oBtnSetWidth=document.getElementById("btnSetWidth");//修改层的宽度
			var oDiv=document.getElementById("div1");
			oBtnGetColor.οnclick=function(){//获取层的颜色
				show(oDiv);
			};
			oBtnSetColor.οnclick=function(){
				objectStyle(oDiv,"background-color","green");//修改层的颜色
				
			};
			oBtnSetWidth.οnclick=function(){//修改层的宽度
				objectStyle(oDiv,"width","200px");
			};

		};
		
	</script>
	<body>
			<input id="btnGetColor" type="button" value="获取层的颜色" />
			<input id="btnSetColor" type="button" value="修改层的颜色" />
			<input id="btnSetWidth" type="button" value="修改层的宽度" />
			<div id="div1">
			</div>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值