input按钮样式美化

对input按钮样式的美化,思路有两个:

  • 1.将input元素的opacity设置为0,外面用div包裹。视觉上看不到input元素,但点击动作还是在input上
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    .box{
        position: relative;
        width: 200px;
        height: 50px;
        line-height: 50px;
        background-color: pink;
        color: #fff;
        text-align: center;        
    }
    .input{
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        right: 0;
        opacity: 0;
    }
    </style>
</head>
<body>
    <div class="box" id="uploadBox">
        <span>选择文件</span>
        <input class="input" id="uploadInput" type="file" multiple onchange="upload"/>
    </div>   
</body>
</html>
  • 2.直接将input元素隐藏:display:none,然后将点击其余DOM时,触发input元素的click事件即可。那个显性显示的DOM元素的样式爱怎么写怎么写。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>    
        .box{
            width: 200px;
            height: 50px;
            line-height: 50px;
            background-color: pink;
            color: #fff;
            text-align: center;                
        }
        .input{
            display: none;
        }
    
    </style>
</head>
<body>
    <div class="box" id="uploadBox">
        <span>选择文件</span>
        <input class="input" id="uploadInput" type="file" multiple onchange="upload"/>
    </div>

    <script>   
        var domUploadBox = document.getElementById('uploadBox')
        var domUploadInput = document.getElementById('uploadInput')
        
        domUploadBox.addEventListener('click', refsInput)

        function refsInput () {
           domUploadInput.click();
        } 
    </script>  
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,814评论 1 92
  • //------------------------- 第一章 认识JQuery ----------------...
    米塔塔阅读 748评论 0 9
  • 一、样式篇 第1章 初识jQuery (1)环境搭建 进入官方网站获取最新的版本 http://jquery.co...
    凛0_0阅读 3,466评论 0 44
  • 选择器选择器是jQuery的核心。 事件 动画 扩展
    wyude阅读 490评论 0 1
  • ——释失忆 掌心的温度给你 耳畔的呢喃给你 发辫的轻抚给你 零度的亲吻给你 只要是你 初遇停格在稷下时光里 新日的...
    无良雨落_阅读 1,098评论 0 0