javascript中with语句应用

语法格式:
  with(obj){}
  obj指明了语句组中对象缺省时的参考对象,就是代表该语句块中的默认作用域为obj。

<html>
<head>
    <title></title>
    <meta charset="utf-8"/>
    <script type="text/javascript">
      function test(){
        var obj=document.getElementById("test");
        obj.className="red";
        obj.value="not use with";
      }
      function testWith(){
        var a=document.getElementById("test");
        with(a){
            value="using  with";
            className="blue";
        }
      }
    </script>
    
    <style type="text/css"> 
    .red{background:red;color:#ffffff} 
    .blue{background:blue;color:yellow} 
    </style> 

</head>
<body>
    <input id="test" type="text" name="test" value=""/> 
    <input type="button" value="Changed" onclick="test()"/> 
    <input type="button" value="withChanged" onclick="testWith()"/> 
</body>
</html>

 

posted @ 2014-07-04 15:32  以梦为码  阅读(190)  评论(0编辑  收藏  举报