日期:2014-05-16  浏览次数:20358 次

js中是不是可以省略getElementById,下面的代码有没有注释部分都照样成功运行啊
<head>
  <title>无标题页</title>
  <style type="text/css">
  .day{background-color:Blue;}
  .night{background-color:Black;}
  </style>
  <script type="text/javascript">
  function switchLight(){
  //var btnSwitch=document.getElementById("btnSwitch");
  if(document.body.className=="night"){
  document.body.className="day";
  btnSwitch.value="关灯"
  }
  else{
  document.body.className="night";
  btnSwitch.value="开灯";
  }
  }
  </script>
</head>
<body class="day">
  <input type="button" id="btnSwitch" value="关灯" onclick="switchLight()" />
</body>
</html>

------解决方案--------------------
不可以省略
当你使用服务器控件或母版页后,就不是这样简单的付值就可以的了
------解决方案--------------------
要使用安全可靠兼容的方法
------解决方案--------------------
不是标准的做法
某些浏览器不支持这种写法的 ,如firefox