看到這麼一篇文章:http://www.tuicool.com/articl...
程式碼片段:
<script>
function css()
{
if(arguments.length==2) //获取
{
return arguments[0].style[arguments[1]];
}
else
{
arguments[0].style[arguments[1]]=arguments[2];
}
}
window.onload=function ()
{
var op=document.getElementById('p1');
alert(css(op, 'width')); //获取width属性值
css(op, 'background', 'green');
};
</script>
<p id="p1" style="width:200px; height:200px; background:red;"></p>
範例2這麼一行:return arguments[0].style[arguments[1]];
為什麼不是 這樣的寫法? return arguments[0].style.arguments[1];
不是用點而是用的[],陣列?
.
運算子存取屬性時,屬性名是寫定的,不能夠根據表達式動態的生成屬性名的.[]
運算子存取屬性時,可以根據[的表達式動態產生屬性名,得到屬性名稱這個字串
.style.arguments[1]
相當於style['arguments'][1]