js中[]与.用法

来源:互联网 发布:三维软件开发工具 编辑:程序博客网 时间:2024/04/26 23:10
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>js中[]与.用法</title>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
overflow: hidden;
}
.wrap{
width: 100%;
height: 100%;
padding: 100px;
background: darkgrey;
color: white;
}
.test{
width: 300px;
height: 300px;
background: white;
}
</style>
<script>
window.onload=function(){
var oName=document.getElementById('attrName'),
oValue=document.getElementById('attrVal'),
oButton=document.getElementById('btn'),
oTest=document.getElementById('test');
oButton['onclick']=function(){          //oButton['onclick']与oButton.onclick 是相等的意思;
oTest.style[oName.value]=oValue.value;//oName.value是个动态值,而style.后面是个固定值;
//[]能代替.,而.不能代替[]!!!!
}
}
</script>
</head>
<body>
<div class="wrap">
属性名:<input type="text" name="" id="attrName" /><br /><br />
属性值:<input type="text" name="" id="attrVal" />
<input type="button" value="确定" id="btn"/><br /><br />
<div class="test" id="test"></div>
</div>
</body>
</html>
0 0