让前端飞Web前端之路

搜索文本框

2019-10-25  本文已影响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>

上一篇 下一篇

猜你喜欢

热点阅读