jQuery事件4-3练习

来源:4-3 编程练习

深海皮皮虾

2019-06-09 15:12:18

<!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() {
            
            $("input").focus(function() {
                $("span").text("请输入用户名");
            })
            
            $("input").blur(function() {
                if($("input").val() == "") {
                    $("span").text("用户名不能为空");
                    $(this).css({"border-color": "red"});
                } else {
                    $("span").text("");
                    $(this).css({"border-color": "green"});
                }
                
            })
        })
   
    </script>
</body>

</html>

请老师检查

写回答

1回答

好帮手慕言

2019-06-09

同学你好,问题:当内容为空失去焦点时,边框变为红色,再次聚焦时边框是红色与蓝色。

http://img.mukewang.com/climg/5cfcbe870001ea1e02900041.jpg

解决方法:再次聚焦时,设置一下边框颜色

代码建议参考:

 http://img.mukewang.com/climg/5cfcbf55000109fe08780728.jpg

如果帮助到了你,欢迎采纳,祝学习愉快~

 


0

0 学习 · 36712 问题

查看课程