focus只是在每次获得焦点的时候判断一下是吗?

来源:4-3 编程练习

Aurora_Meteor

2020-03-22 21:21:09

<!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>

    //此处写代码

   $('input').focus(function(){

       $(this).css({'borderColor':'blue'});

       if($(this).val()==''){

           $('span').text('请输入用户名');

       }else{

           $('span').text('');

       }

   });

   $('input').blur(function(){

       if($(this).val()==''){

           $(this).css({'borderColor':'red'});

           $('span').text('用户名不能为空');

       }else{

           $(this).css({'borderColor':'green'});

           $('span').text('');

       }

   });

    </script>

</body>


</html>

点击输入框以后显示了‘请输入用户名’,输入以后提示不会消失,只有输入框失去焦点以后执行了blur函数提示才会消失,它不能在输入框有内容的时候自动消失啊?

写回答

1回答

好帮手慕码

2020-03-23

同学你好,效果是正确的。另,“focus只是在每次获得焦点的时候判断”是的,只有在相应的事件中执行相应的脚本。

输入内容的时候,提示框是不会自动消失的。只有在失去聚焦的时候判断val是否有值才会改变提示框的内容:

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

如果我的回答帮到了你,欢迎采纳,祝学习愉快~

0

0 学习 · 14456 问题

查看课程