標籤:
<!--
with 語句可以方便地用來引用某個特定對象中已有的屬性,但是不能用來給對象添加屬性。要給對象建立新的屬性,必須明確地引用該對象。
with(object instance){
//代碼塊
}
有時候,在一個程式碼中,多次需要使用某對象的屬性或方法,照以前的寫法,都是通過:對象.屬性或者對象.方法這樣的方式來分別獲得該對象的屬性和方法,著實有點麻煩,學習了with語句後,可以通過類似如下的方式來實現:
with(objInstance){
var str = 屬性1;
.....
} 去除了多次寫對象名的麻煩。
-->
<!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>2016-04-06 JavaScript--with()的用法</title>
<script type="text/javascript">
function tform(thisform){
with(thisform){
if(thisform.user.value==null||thisform.user.value==""){
alert("使用者名稱為空白");
return false;
}else if(psw.value==null||psw.value==""){
alert("密碼為空白");
return false;
}else{
return true;
}
}
}
</script>
</head>
<body>
<form action="Main.html" onsubmit="return tform(this)" method="post">
使用者:<input type="text" name="user"/>
密碼:<input type="password" name="psw"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
2016-04-06 JavaScript--with()的用法