首页 > 其他 > 详细

仿某东的搜索框初级版

时间:2019-11-09 09:35:45      阅读:119      评论:0      收藏:0      [点我收藏+]

首先需要获取焦点事件 onfocus和失去焦点事件 onblur

技术分享图片

 

 

 

 下面是代码环节:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <style type="text/css">
        input{
            color: #999;
            outline:2px solid red;
            outline-offset: 2px;
            border: none;
            display: block;
            margin:50px auto;
        }
    </style>
    <title>Document</title>
</head>
<body>
    
    <input type="text" name="" id="" value="手机" />
    <script type="text/javascript">
//        获取元素
        var ipt = document.getElementsByTagName("input");
//        注册事件 获取焦点事件 onfocus
        ipt[0].onfocus = function(){            
//            alert("得到了焦点");实验一下能不能得到焦点
            if(ipt[0].value === "手机"){
            ipt[0].value = ""
        }
        
        ipt[0].setAttribute("style","color: #333;");
        }
//        注册事件 失去焦点事件 onblur
        ipt[0].onblur = function(){
//            alert("失去焦点");实验一下能不能失去焦点
            if(ipt[0].value === ""){
            ipt[0].value = "手机"
        }
            ipt[0].setAttribute("style","color: #999;");
        }
    </script>
</body>
</html>

某东效果图

技术分享图片

 

 

 

仿某东的搜索框初级版

原文:https://www.cnblogs.com/niuyaomin/p/11824012.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!