在我们项目用户注册板块时,通常会在客户端对用户填写 用户名,密码等其他基本资料时做些判断,比如不能为空,符合某一长度等等,通常我们的做法是当不符合时,就alert('some messages...');这样对客户来讲总是感觉不太好友,这时我们可以换另外一种提示方法,非弹出示的,即用
innerHTML来实现,
说明:
innerHTML 属性的使用非常流行,因为他提供了简单的方法完全替代一个 HTML 元素的内容。另外一个方法是使用 DOM Level 2 API (removeChild, createElement, appendChild)。但很显然,使用 innerHTML 修改 DOM tree 是非常容易且有效的方法.
基本用法:document.控件name.innerHTML("内容") .
<script>
function validate()

{
document.getElementById("udiv").innerHTML="";
document.getElementById("pdiv").innerHTML="";

var isok = true;
if(document.getElementById("userName").value=="")

{
document.getElementById("udiv").innerHTML="用户名不能为空";
isok = false;
}
if(document.getElementById("phone").value.length!=11)

{
document.getElementById("pdiv").innerHTML="长度应该为11位";
isok = false;
}
return isok;
}
</script>
<form action="test.html" name="frm" onsubmit="return validate()">
用户名:<input type="text" name="userName" id="phone"><span id="udiv"></span><br>
手机:<input type="text" name="phone" id="phone"><span id="pdiv"></span><br>
<input type="submit" value="提 交">
</form>

这样比总是弹出提示界面要友好多了.
posted on 2008-09-11 11:29
henry1451 阅读(566)
评论(0) 编辑 收藏