javascript中with语句应用

时间:2023-02-15 10:09:29
语法格式:
  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>