问题:
首先对于alert() 有时候有提示 有时候不出来的除了alert()提示另外的 一个效果都没有出现 本来是在自己写的小项目中开始的时候有掉过出现的 后来不知道怎么回事就没效果了然后写个小程序在新建的项目中测试下 就是上面的代码
然后除了 alert 有效果 另外的 没有
把 alert 那几行代码删除了 另外的效果也没有出现 会不会与浏览器有关系 是谷歌浏览器 貌似IE也没效果如果是代码错误 请提示下:坐等!
jspJavaScriptEclipse
首先对于alert() 有时候有提示 有时候不出来的除了alert()提示另外的 一个效果都没有出现 本来是在自己写的小项目中开始的时候有掉过出现的 后来不知道怎么回事就没效果了然后写个小程序在新建的项目中测试下 就是上面的代码
然后除了 alert 有效果 另外的 没有
把 alert 那几行代码删除了 另外的效果也没有出现 会不会与浏览器有关系 是谷歌浏览器 貌似IE也没效果如果是代码错误 请提示下:坐等!
jspJavaScriptEclipse
第一个 除了alert 另外几个是出不来效果的 代码貌似应该没有错的...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body>
<div>
username:<input type="text" id="username" tabindex="1" onblur="checkUsername()"/><br/>
password1:<input type="password" id="psw1" tabindex="2" /><br/>
password2:<input type="password" id="psw2" tabindex="3" onchange="checkPsw()"/><br/>
</div>
<script type="text/javascript">
function checkUsername(){
//alert('why1');
if(document.getElementById("username").value==""){
document.getElementById("username").setAttribute('value',"不能为空");
document.getElementById("username").focus();
}
}
function checkPsw(){
if(document.getElementById("psw1").value != document.getElementById("psw2").value){
//alert('why2');
document.getElementById("psw2").setAttribute('value',"不能为空");
document.getElementById("psw2").focus();
}
}
/*
Object.defineProperty(String.prototype, "type", {
get: function() {
return /^\d+$/.test(this)?'Number':(/^\d{4}(-\d{1,2}){2}/.test(this)?'Date':'String');
}
});
*/
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function checkUsername(ele){
if(document.getElementById("username").value==""){
document.getElementById("username").setAttribute('placeholder',"不能为空");
//fix mozilla element focus bug
setTimeout(function(){
ele.focus();
});
}
}
function checkPsw(){
if(document.getElementById("psw1").value != document.getElementById("psw2").value){
document.getElementById("checkpassword").innerHTML="两次输入不一致";
document.getElementById("psw2").focus();
}
}
/*
Object.defineProperty(String.prototype, "type", {
get: function() {
return /^\d+$/.test(this)?'Number':(/^\d{4}(-\d{1,2}){2}/.test(this)?'Date':'String');
}
});
*/
</script>
</head><body>
<div>
<form name="f2">
username:<input type="text" name="username" id="username" tabindex="1" onblur="checkUsername(this)"/><br/>
password1:<input type="password" id="psw1" tabindex="2" /><br/>
password2:<input type="password" name="psw2" id="psw2" tabindex="3" onkeypress="checkPsw()"/><br/>
<label id="checkpassword"></label>
</form>
</div></body>
</html>ie不支持placeholder,可以下个placeholder.js。
showbo的意思是<input不可以用innerHTML赋值
showbo的意思是<input不可以用innerHTML赋值
你这么一说 我查了下,原来是我的innerHTML用错了, innerHTML用在标签上的 input 不能这样用的
终于弄懂了 还是很感谢你的 细心解答 和贴了代码。
自己修改之后 可以达到效果了。<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
<script type="text/javascript">
function checkUsername() {
if(document.getElementById("username").value == "") {
document.getElementById("username").focus();
document.getElementById("user").innerHTML= "<font color=red>用户名不能为空</font>";
}
}
function checkPsw() {
if(document.getElementById("psw1").value != document.getElementById("psw2").value) {
document.getElementById("psw2").value = "";
document.getElementById("psw2").focus();
document.getElementById("psw").innerHTML= "<font color=red>两次密码不一致</font>";
}
}
</script>
</head>
<body>
<div>
username : <input type="text" name="username" id="username" onblur="checkUsername()"/><span id="user"></span> <br>
password1 : <input type="password" name="psw1" id="psw1"> <br>
password2 : <input type="password" name="psw2" id="psw2" onchange="checkPsw()"> <span id="psw"></span><br>
</table>
</body>
</html>