<!DOCTYPE html>
<html>
<head>
<title>函数传参2</title>
<style type="text/css">
#div1{
width: 200px;
height: 200px;
background: #aaa;
}
</style>
<script type="text/javascript">
function setStyle(paramName,paramValue)
{
/**
* 所有用 . 号的地方都可以用[] 代替
* oDiv.style['width'] <=> oDiv.style.width
* oDiv[style]['width'] <=> oDiv.style.width
* 用方括号的好处是可以在方括号中使用变量,动态指定要改变的值的类型
* @type {[type]}
*/
var oDiv = document.getElementById("div1");
oDiv.style[paramName] = paramValue;
}
function toDefault()
{
var oDiv = document.getElementById("div1");
oDiv.style.background = '#aaa';
oDiv.style.width = '200px';
oDiv.style.height = '200px';
}
</script>
</head>
<body>
<input type="button" value="green" οnclick="setStyle('background','green')">
<input type="button" value="bule" οnclick="setStyle('background','blue')">
<input type="button" value="yellow" οnclick="setStyle('background','yellow')">
<input type="button" value="width" οnclick="setStyle('width','400px')">
<input type="button" value="height" οnclick="setStyle('height','400px')">
<input type="button" value="Default" οnclick="toDefault()">
<div id="div1">
</div>
</body>
</html>
JavaScript函数传参
最新推荐文章于 2024-05-12 16:03:47 发布