<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>注册页面</title>
<script>
function checkInfo()
{
var eM = document.getElementById("eMail");
var userName = document.getElementById("user").value;
var pwd1 = document.getElementById("password1").value;
var pwd2 = document.getElementById("password2").value;
var checkCode = document.getElementById("code").value;
var checkBox = document.getElementById("checkbox");
var tips = document.getElementsByName("tip");
if (eM.value.length == 0)
{
tips[0].innerHTML="<font color='red'>请输入邮箱地址</font>";
}
else if (eM.value.indexOf('@', 0) == -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址应带\"@\"符号</font>";
}
else if (eM.value.indexOf('.', 0) == -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址至少要有一个\".\"符号</font>";
}
else if (eM.value.indexOf('@', eM.value.indexOf('@', 0)) != -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址只能有一个\"@\"符号</font>";
}
else if (userName.length == 0)
{
tips[1].innerHTML="<font color='red'>请输入您的用户名</font>";
}
else if (pwd1.length == 0)
{
tips[2].innerHTML="<font color='red'>请输入您的密码</font>";
}
else if (pwd2.length == 0)
{
tips[3].innerHTML="<font color='red'>请输入您的密码</font>";
}
}
</script>
</head><body>
<form name="myForm" action="">
<table background="素材/background.jpg" width="1265" height="693">
<tr>
<td height="122" colspan="4"></td>
</tr>
<tr>
<td width="149" height="55"></td>
<td width="224"></td>
<td width="222" style="padding-top:28px; padding-bottom:0px"><input type="text" id="eMail" style="width:201px; height:25px" onBlur="checkInfo()"></td>
<td width="650" style="padding-top:28px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="user" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:30px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="password1" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:30px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="password2" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:28px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="60"></td>
<td></td>
<td style="padding-top:33px; padding-bottom:0px;"><input type="text" id="code" style="width:90px; height:25px;"></td>
<td></td>
</tr>
<tr>
<td height="41"></td>
<td></td>
<td style="padding-top:18px; padding-bottom:0px"><input type="checkbox" id="checkbox" value="checkbox">
<font size="-1" color="#666666">用该邮箱创建账户</font></td>
<td></td>
</tr>
<tr>
<td height="54"></td>
<td></td>
<td style="padding-top:17px; padding-bottom:0px;"><img src="素材/buttom.jpg" width="224" height="37"></td>
<td></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
</table>
</form>
</body>
</html>正在学习JS,做了个表单验证,有些问题不太明白,我用了一个数组tips,来存放getElementsByName获取tip元素对象集(也就是下面的div标签),再通过这个tips数组的下标去改变对应的innerHTML内容,这可以的吗?难道innerHTML只能通过ID来访问修改吗?请各位指教一下,谢谢!
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>注册页面</title>
<script>
function checkInfo()
{
var eM = document.getElementById("eMail");
var userName = document.getElementById("user").value;
var pwd1 = document.getElementById("password1").value;
var pwd2 = document.getElementById("password2").value;
var checkCode = document.getElementById("code").value;
var checkBox = document.getElementById("checkbox");
var tips = document.getElementsByName("tip");
if (eM.value.length == 0)
{
tips[0].innerHTML="<font color='red'>请输入邮箱地址</font>";
}
else if (eM.value.indexOf('@', 0) == -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址应带\"@\"符号</font>";
}
else if (eM.value.indexOf('.', 0) == -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址至少要有一个\".\"符号</font>";
}
else if (eM.value.indexOf('@', eM.value.indexOf('@', 0)) != -1)
{
tips[0].innerHTML="<font color='red'>您的邮箱地址不正确,邮箱地址只能有一个\"@\"符号</font>";
}
else if (userName.length == 0)
{
tips[1].innerHTML="<font color='red'>请输入您的用户名</font>";
}
else if (pwd1.length == 0)
{
tips[2].innerHTML="<font color='red'>请输入您的密码</font>";
}
else if (pwd2.length == 0)
{
tips[3].innerHTML="<font color='red'>请输入您的密码</font>";
}
}
</script>
</head><body>
<form name="myForm" action="">
<table background="素材/background.jpg" width="1265" height="693">
<tr>
<td height="122" colspan="4"></td>
</tr>
<tr>
<td width="149" height="55"></td>
<td width="224"></td>
<td width="222" style="padding-top:28px; padding-bottom:0px"><input type="text" id="eMail" style="width:201px; height:25px" onBlur="checkInfo()"></td>
<td width="650" style="padding-top:28px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="user" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:30px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="password1" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:30px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="61"></td>
<td></td>
<td style="padding-top:34px; padding-bottom:0px;"><input type="text" id="password2" style="width:201px; height:25px;" onBlur="checkInfo()"></td>
<td style="padding-top:28px; padding-bottom:0px; font-size:16px"><div name="tip"></div></td>
</tr>
<tr>
<td height="60"></td>
<td></td>
<td style="padding-top:33px; padding-bottom:0px;"><input type="text" id="code" style="width:90px; height:25px;"></td>
<td></td>
</tr>
<tr>
<td height="41"></td>
<td></td>
<td style="padding-top:18px; padding-bottom:0px"><input type="checkbox" id="checkbox" value="checkbox">
<font size="-1" color="#666666">用该邮箱创建账户</font></td>
<td></td>
</tr>
<tr>
<td height="54"></td>
<td></td>
<td style="padding-top:17px; padding-bottom:0px;"><img src="素材/buttom.jpg" width="224" height="37"></td>
<td></td>
</tr>
<tr>
<td colspan="4"></td>
</tr>
</table>
</form>
</body>
</html>正在学习JS,做了个表单验证,有些问题不太明白,我用了一个数组tips,来存放getElementsByName获取tip元素对象集(也就是下面的div标签),再通过这个tips数组的下标去改变对应的innerHTML内容,这可以的吗?难道innerHTML只能通过ID来访问修改吗?请各位指教一下,谢谢!
可以通过
var tips = document.getElementsByName("tip");
var tips = document.getElementsById("tip");
var tips = document.getElementsByTagName("div ");
如果tips[0].innerHTML用法没问题,请问那里出错了,请高人指点一下!谢谢!