使用JQuery实现输入框placeholder效果

<head>
  <meta charset="utf-8">
  <title>使用JQuery练习placehold</title>
  <script type="text/javascript" src="jquery-3.3.1.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
        
      $("input").blur(function(event) {
          //当失去焦点,判断当前值是否为空
        if ($(this).val() == "") {
            //如果为空,则返回默认值
          $(this).val(this.defaultValue);
        }
      });
      $("input").focus(function(event) {
          //当点击输入框时,首先判断当前文本框的值是不是默认值
        if ($(this).val() == "请输入账号"||$(this).val() == "请输入密码") {
            //如果是则清空
          $(this).val("");
        }else {
            //如果不是,则保留当前输入的值
          $(this).val(this.value);
        }
      });
    })
  </script>
</head>
<body>
  <div class="all">
    账户:<input type="text" name="1" value="请输入账号" >
    密码: <input type="mima" name="2" value="请输入密码">
  </div>
</body>

猜你喜欢

转载自blog.csdn.net/weixin_42886104/article/details/86475455