HTML之文本框关键字显示

文本框默认显示 “请输入关键字”,当鼠标点击输入框的时候, “请输入关键字”这几个字消失,移出文本框又显示出来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div style="width: 600px;margin: 0 auto;">
        <input id="i1" onfocus="Focus();" onblur="Blur();" type="text" value="请输入关键字"/>

        <input type="text" placeholder="请输入关键字" />
    </div>

    <script>
        function Focus(){
            var tag = document.getElementById('i1');
            var val = tag.value;
            if(val == "请输入关键字"){
                tag.value = "";
            }
        }
        function Blur(){
            var tag = document.getElementById('i1');
            var val = tag.value;
            if(val.length ==0){
                tag.value = "请输入关键字";
            }
        }
    </script>
</body>
</html>
posted @ 2016-11-24 12:02  Dus  阅读(1034)  评论(0编辑  收藏  举报