老师,为什么我这样写不对呢
来源:4-3 编程练习
qq_NDG_0
2019-02-23 09:50:11
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>习题</title>
</head>
<body>
<input type="text" value=""><span></span>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
//此处写代码
$(document).ready(function(){
var i = $("input").val().length;
$("input").focus(function(){
$("span").text("请输入用户名");
});
$("input").blur((function(){
if(i>0){
$(this).css("border-color","green");
$("span").text("");
}else{
$(this).css("border-color","red");
$("span").text("用户名不能为空");
}
}))
})
</script>
</body>
</html>1回答
同学你好,代码中有一个小问题:
1、i值存放的是输入内容的长度,只有在失去焦点的时候获得,参考下图:

自己测试一下哦,如果帮助到了你,欢迎采纳。
祝学习愉快!
相似问题