口袋妖怪黑白2毒系:为什么这个函数放在表单下面就好用,放在表单上面就不好用呢?

来源:百度文库 编辑:高考问答 时间:2024/04/30 15:56:28
如:
<body>
<form name=form1 action="">
<input type=text name=a>
<input type=button value=点这里隐藏文本框 onclick="hiddenElement();">
<input type=button value=点这里显示文本框 onclick="showElement();">
</from>
<script language="vbscript">
function hiddenElement()
form1.a.style.display="none"
end function
function showElement()
form1.a.style.display=""
end function
</script>
</body>
这样就好用,可是下面就不好用:(
<body>
<script language="vbscript">
function hiddenElement()
form1.a.style.display="none"
end function
function showElement()
form1.a.style.display=""
end function
</script>
<form name=form1 action="">
<input type=text name=a>
<input type=button value=点这里隐藏文本框 onclick="hiddenElement();">
<input type=button value=点这里显示文本框 onclick="showElement();">
</from>
</body>
谁能告诉我这是怎么会事啊!

</from> 写错了,应该是</form>.
这就可以解释了,因为在form1外面的函数找不到form1.a,因为form1未结束,在结束之前外部的函数无法找到它
在里面不管是否结束都可以找到,因为是包含在form1中.

首先 你的两段代码都有问题,onclick="hiddenElement();" 这里用的javascript语法,而到hiddenElement函数的时候去用的 是vbscript语法,建议去掉onclick中的分号,后面加上language="vbscript"

其次 有些页面脚本会发生因位置不同而出错的现象,一般是因为代码运行的时候页面还没有加载完毕。