搜索文本框

作者: 广陵周惊蛰 | 来源:发表于2019-10-25 19:12 被阅读0次

效果描述:输入状态和为输入状态切换

原始输入框.png 输入中.png

源代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .gray {
      color: gray;
    }
    .black {
      color: black;
    }
  </style>
</head>
<body>
  <input type="text" class="gray" value="请输入搜索关键字" id="txtSearch"> <input type="button" value="搜索" id="btnSearch">

  <script>
    var txtSearch = document.getElementById('txtSearch');
    txtSearch.onfocus = function () {
      if (this.value === '请输入搜索关键字') {
        this.value = '';
        this.className = 'black';
      } 
    }

    txtSearch.onblur = function () {
      if (this.value.length === 0 || this.value === '请输入搜索关键字') {
        this.value = '请输入搜索关键字';
        this.className = 'gray';
      }
    }
  </script>
</body>
</html>

相关文章

网友评论

    本文标题:搜索文本框

    本文链接:https://www.haomeiwen.com/subject/lrjevctx.html