JS 输入框为空的使用

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
<meta charset="utf-8" />
    <script>
        onload = function () {
            var txtObj = document.getElementById('txt');
            txtObj.onfocus = function () {
                if (this.value == '请输入搜索关键词' && this.style.color=='gray') {
                    this.value = '';
                    this.style.color = 'black';
                }

            }


            txtObj.onblur = function () {
                if (this.value == '') {
                    this.style.color = 'gray';
                    this.value = '请输入搜索关键词';
                }

            }
        }
    </script>
</head>
<body>
    <input type="text" value="请输入搜索关键词" style="color:gray" id="txt"/>
 </body>
</html>
posted @ 2018-03-20 22:04  dxm809  阅读(175)  评论(0编辑  收藏  举报