在JavaScript中操作属性的两种方法

JavaScript对于大家来说应该都不是很陌生  今天说的就是改变元素属性的两种方式

第一种:<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>第一种</title>
  <style>
  #shuxin{
 width:200px;
 height:200px;
 background-color:black;
 border:1px solid silver;
  }
  </style>
 </head>
 <body>
  <div id="shuxin"></div>
  <input type="button" value="yellow" onclick="shuxin('yellow');">
  <input type="button" value="blue" onclick="shuxin('blue');">
  <input type="button" value="white" onclick="shuxin('white');">
  <script>
 function shuxin(color){
 var i=document.getElementById('shuxin');
 i.style.background=color;
 }
  </script>
 </body>
</html>

这种情况主要是: i.style.background=color;的形式改变属性的值

但是只能改变一种属性的值 为了改变属性和属性的值则产生了第二种方法

 i.style【background】=color; 换做变量则   i.style[属性名称变量]=属性值得变量

方法体则可以写成shuxin(name,color)

如下代码:

posted @ 2016-01-13 12:25  谢玉胜  阅读(164)  评论(0编辑  收藏  举报
@allenXieyusheng