传不定参:arguments,参数的个数可变;(例子:css函数
2017-11-16 本文已影响14人
想做一个画家
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="div1" style="width:200px; height:200px; background:red"></div>
</body>
<script>
var oDiv = document.getElementById('div1')
function css(obj,name,value) {
if (arguments.length == 2) {
// 获取样式
return obj.style[name];
}
else {
// 设置样式
obj.style[name] = value;
}
};
alert(css(oDiv, 'width'))
css(oDiv, 'background' ,'yellow')
</script>
</html>
获取样式.png
设置样式.png