先来看看这样一段代码:
<HTML>
<HEAD>
<script language="javascript">
<!--Begin Code
//******************
//取得两个text的value 并写在页面上
//******************
function GetValue1()
{
document.write("Name:"+yidishui.value);
document.write("Age:"+yidishui1.value);
}
function GetValue2()
{
var name=yidishui.value;
var age=yidishui1.value;
document.write("Name:"+name);
document.write("Age:"+age);
}
//End Code
// -->
</script>
</HEAD>
<BODY>
Name:<input type="text" name="yidishui"/><br>
Age: <input type="text" name="yidishui1"/><br>
<input type="button" onclick="GetValue1()" value="call->GetValue1"/> <input type="button" onclick="GetValue2()" value="call->GetValue2"/>
</BODY>
</HTML>
<HEAD>
<script language="javascript">
<!--Begin Code
//******************
//取得两个text的value 并写在页面上
//******************
function GetValue1()
{
document.write("Name:"+yidishui.value);
document.write("Age:"+yidishui1.value);
}
function GetValue2()
{
var name=yidishui.value;
var age=yidishui1.value;
document.write("Name:"+name);
document.write("Age:"+age);
}
//End Code
// -->
</script>
</HEAD>
<BODY>
Name:<input type="text" name="yidishui"/><br>
Age: <input type="text" name="yidishui1"/><br>
<input type="button" onclick="GetValue1()" value="call->GetValue1"/> <input type="button" onclick="GetValue2()" value="call->GetValue2"/>
</BODY>
</HTML>
看上去两个函数达到的是同一个效果,但是不然。
调用函数GetValue1()运行是达不到效果,欢鳪etValue2()可以实现
。
。
。
===============
调用GetValue1() 是出现错误原因 在使用了第二个 是document.write()出错 ,此时的yidishui1对象已经不存在了,因为使用第一个docuemnt.wrte()时重写了整个页面,所以用的二个时yidishui1对象不存在了!!!
大家可以看看英文对docuemnt.write()的解释:
You should not use the write or writelen methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables