html 当文本框未输入时点按钮,旁边出现红色警告字怎么写求代码

不要弹出框
例如这样

<html>
<head>
<meta http-equiv=content-type content="text/html;charset=GBK">
</head>
<body>
<form action="" method="post" onsubmit="return check(this)">
<label id="msg" style="color:red;font-size:12pt;"></label>
<br/>
用户名:<input name="username"/>
<br/>
密 码:<input type="password" name="username"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
<script>
function check(form){
return (function(node){
// 如果已经没有需要校验的子节点,则推出递归
if(!node) return true;
// 判断子节点类型,这里我们只需要校验text和password类型文本
if(/((text)|(password))/i.test(node.type)){
var isEmpty = node.value.length == 0;
// 校验文本是否为空
if(isEmpty){
caution(node); return false;
}
}
// 使用递归来遍历下一个节点
return arguments.callee(node.nextSibling);
})(form.firstChild);
}
function caution(node){
document.getElementById("msg").innerHTML = "请填写账号";
node.focus();
}
</script>
</html>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-22
js判断是否为空 ,为空的时候高亮显示。网上有你自己搜下想要的代码就是了。
第2个回答  2013-03-22
代码到这里去看 http://jsfiddle.net/UvKrq/

主要就是上边放一个DIV元素用来占位,然后判断输入框是否输入,然后相应的给DIV里边设置文字就行了。不懂的欢迎继续追问。