JavaScript DOM操作案例模拟搜索框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        input {
            color: gray;
        }
    </style>
</head>
<body>
<input type="text" value="请输入搜索内容" id="tex"/>
<script src="common.js"></script>
<script>
    //注册获取焦点事件
    my$("tex").onfocus = function () {
        if (this.value == "请输入搜索内容") {
            this.value = "";//清空文本框
            this.style.color = "black";
        }
    };
    //注册失去焦点事件
    my$("tex").onblur = function () {
        if (this.value == "") {
            this.value = "请输入搜索内容";
            this.style.color = "gray";
        }
    };
</script>
</body>
</html>

 

posted @ 2018-07-23 21:30  {颜逸}  阅读(304)  评论(0)    收藏  举报