HTML中文件上传时使用的<input type=“file“>元素的样式自定义

今天分享下”“这篇文章,文中根据实例编码详细介绍,或许对大家的编程之路有着一定的参考空间与使用价值,需要的朋友接下来跟着云南仟龙Mark一起学习一下吧。 Web页面中,在需要上传文件时基本都会用到元素,它的默认样式:

chrome下:

在这里插入图片描述

IE下:

在这里插入图片描述

论是上边哪一种,样式都比较简单,和许多网页页面的风格也不太融洽。

依据使用者的要求,设计方案风格,更改其表明样式的场所就比较多了。

假如,要像下边一样做一个bootstrap风格的提交按键该如何快速完成。

在这里插入图片描述

搭建上传按钮所需的基本元素

复制代码

代码如下:

<span> <span>上传</span> <input type="file"> </span>

效果(chrome):

现在看到的分两行显示。

外围之所以没有换成div,是因为在IE7-浏览器中,只要不是设成inline,它的宽度全都会撑开到能撑到的宽度。如果设成inline,那元素的宽度就无法调整,所以这里用span然后设成inline-block能解决这样的问题。

增加样式将两行变成一行

复制代码

代码如下:

<span"> <span>上传</span> <input type="file"> </span>

css:

复制代码

代码如下:

.fileinput-button { position: relative; display: inline-block; } .fileinput-button input{ position: absolute; right: 0px; top: 0px; }

效果:

默认是没有浅蓝色边框,只有鼠标去点击后,才会显示,这里显示出来是为了看得清楚。

通过将外围的span设成display:relative,将input设成display:absolute的方式让他们都脱离文档流。

通过将input限定在外围的span中进行绝对定位的方式让本来两行显示的变成一行显示。

实际上这里已经overflow了,真正的宽度是“上传”文字的宽度,修改fileinput-button样式增加overflow: hidden

复制代码

代码如下:

.fileinput-button { position: relative; display: inline-block; overflow: hidden; }

效果:

很有意思,能看到上边后右边的蓝色边框了吧,其实就是把左边和下边的溢出部分给隐藏了。

这时候用鼠标去点击“上传”两个字实际上是点在input上,能够显示“打开”对话框,因为显示层级上input要比“上传”更靠近用户。

注意input定位中的right,为什么不用left定位。

当我们改成left后。

效果(chrome):

效果(IE):

在chrome下input元素中的选择按钮露出来,但是没关系,可以通过后面的设透明的方式把它透明掉。

但是在IE下确是会把输入框露出来,关键是鼠标移到输入框上时,指针会变成输入状态,这个就很没法处理了。

通过right的定位方式把输入框移到左边去的方式,可以在IE下回避出现鼠标指针变成输入态的情况。

透明input元素

css:

复制代码

代码如下:

.fileinput-button { position: relative; display: inline-block; overflow: hidden; } .fileinput-button input{ position: absolute; left: 0px; top: 0px; opacity: 0; -ms-filter: 'alpha(opacity=0)'; }

效果:

input完全不见了踪影,点击“上传”依然有效。

可以支持IE8+。

引入bootstrap,并添加按钮样式

head中增加外部css和js的引用。

复制代码

代码如下:

<link rel="stylesheet" href="bootstrap/bootstrap.css"> <link rel="stylesheet" href="bootstrap/bootstrap-theme.css"> <script src="bootstrap/jquery-1.10.2.js"></script> <script src="bootstrap/bootstrap.js"></script>

增加按钮样式。

复制代码

代码如下:

上传

效果:

解决大小问题

如果为fileinput-button样式增加width:100px,将外围的span设成宽100px,会发现点击下部是没有反应的,原因就是input是默认大小,无法覆盖下部。

可以通过为input设置一个很大的字号将其撑大的方式来解决覆盖问题,这里就设个200px。

复制代码

代码如下:

.fileinput-button input{ position:absolute; right: 0px; top:0px; opacity: 0; -ms-filter: ‘alpha(opacity=0)’; font-size: 200px; }

这样就能解决覆盖问题。

完成。

参考:jQuery-File-Upload

如果是要兼容IE7-可以参考jQuery-File-Upload中的写法。

代码:

XML/HTML Code复制内容到剪贴板

<!DOCTYPE html>  
<html>  
<head>  
    <title></title>  
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">  
    <link rel="stylesheet" href="bootstrap/bootstrap.css">  
    <link rel="stylesheet" href="bootstrap/bootstrap-theme.css">  
    <script src="bootstrap/jquery-1.10.2.js"></script>  
    <script src="bootstrap/bootstrap.js"></script>  
    <style>  //http://www.qlyl1688.com
        .fileinput-button {   
            position: relative;   
            display: inline-block;   
            overflow: hidden;   
        }   
        .fileinput-button input{   
            position:absolute;   
            right: 0px;   
            top: 0px;   
            opacity: 0;   
            -ms-filter: 'alpha(opacity=0)';   
            font-size: 200px;   
        }   
    </style>  
</head>  
<body style="padding: 10px">  
    <div align="center">  
        <span class="btn btn-success fileinput-button">  
            <span>上传</span>  
            <input type="file">  
        </span>  
    </div>  
</body>  
</html>  

以上是云南仟龙Mark给大家介绍的所有内容,希望对大家有所帮助,如果大家有任何疑问请在脚本之家留言,如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
table { border-collapse: collapse; width: 100%; } td, th { border: 1.5px solid black; padding: 5px; } table img { max-width: 150px; height: auto; } <table id="myTable"> <thead class="sticky-header"> <tr> <td contenteditable="true"><input type="text" oninput="filterTable(0)" placeholder="工艺膜层"></td> <td contenteditable="true"><input type="text" oninput="filterTable(1)" placeholder="AOI Step"></td> <td contenteditable="true"><input type="text" oninput="filterTable(2)" placeholder="不良类型"></td> <td contenteditable="true"><input type="text" oninput="filterTable(3)" placeholder="Layer(Code)"></td> <td contenteditable="true"><input type="text" oninput="filterTable(4)" placeholder="Type"></td> <td contenteditable="true"><input type="text" oninput="filterTable(5)" placeholder="Dpet"></td> <td contenteditable="true"><input type="text" oninput="filterTable(6)" placeholder="Subcode"></td> <td contenteditable="true">Code描述</td> <td contenteditable="true">Image1</td> <td contenteditable="true">Image2</td> <td contenteditable="true">Image3</td> <td contenteditable="true">Image4</td> <td contenteditable="true">Image5</td> <td contenteditable="true">Image6</td> <td contenteditable="true">判定细则</td> </tr> </thead> <tbody> <tr> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;" > </td> <td> <input type="file" onchange="previewImage(this)"onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)"onclick="showPopup(this.src)"style="width: 100px; height: auto;"> </td> <td contenteditable="true">Particle</td> </tr> </tbody> </table>这个表格怎么设置自定义调整列宽
07-16
要设置自定义调整列宽,你可以使用CSS的`table-layout`属性和`width`属性来实现。具体步骤如下: 1. 在CSS样式,将`table-layout`属性设置为`fixed`。这将使表格的列宽度固定。 2. 为每个表格单元格(`td`和`th`)设置一个固定的宽度(或者使用百分比),以确定每列的默认宽度。 3. 在需要调整列宽的地方,添加一个可调整宽度的元素,比如一个拖动条。 4. 使用JavaScript来处理拖动条的事件,并根据拖动条的位置调整表格列的宽度。 以下是一个示例代码段,演示了如何实现自定义调整列宽的功能: ```html <!DOCTYPE html> <html> <head> <style> table { border-collapse: collapse; width: 100%; table-layout: fixed; } td, th { border: 1.5px solid black; padding: 5px; } th { background-color: #f2f2f2; } .column-resizer { background-color: #ddd; width: 6px; height: 100%; position: absolute; right: 0; top: 0; cursor: col-resize; } </style> </head> <body> <table id="myTable"> <thead class="sticky-header"> <tr> <th contenteditable="true" style="width: 150px">工艺膜层</th> <th contenteditable="true" style="width: 150px">AOI Step</th> <th contenteditable="true" style="width: 150px">不良类型</th> <th contenteditable="true" style="width: 150px">Layer(Code)</th> <th contenteditable="true" style="width: 150px">Type</th> <th contenteditable="true" style="width: 150px">Dpet</th> <th contenteditable="true" style="width: 150px">Subcode</th> <th contenteditable="true" style="width: 150px">Code描述</th> <th contenteditable="true" style="width: 150px">Image1</th> <th contenteditable="true" style="width: 150px">Image2</th> <th contenteditable="true" style="width: 150px">Image3</th> <th contenteditable="true" style="width: 150px">Image4</th> <th contenteditable="true" style="width: 150px">Image5</th> <th contenteditable="true" style="width: 150px">Image6</th> <th contenteditable="true" style="width: 150px">判定细则</th> </tr> </thead> <tbody> <tr> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td contenteditable="true">ACT</td> <td contenteditable="true">Particle</td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td> <input type="file" onchange="previewImage(this)" onclick="showPopup(this.src)" style="width: 100px; height: auto;"> </td> <td contenteditable="true">Particle</td> </tr> </tbody> </table> <script> var columnResizer = document.createElement('div'); columnResizer.className = 'column-resizer'; document.addEventListener('DOMContentLoaded', function() { var table = document.getElementById('myTable'); var resizableColumns = table.querySelectorAll('th'); resizableColumns.forEach(function(column) { var resizer = columnResizer.cloneNode(true); column.appendChild(resizer); resizer.addEventListener('mousedown', function(e) { var originalWidth = column.offsetWidth; var startX = e.pageX; document.addEventListener('mousemove', resizeColumn); document.addEventListener('mouseup', stopResize); function resizeColumn(e) { var width = originalWidth + (e.pageX - startX); column.style.width = width + 'px'; } function stopResize() { document.removeEventListener('mousemove', resizeColumn); document.removeEventListener('mouseup', stopResize); } }); }); }); </script> </body> </html> ``` 在这个示例,每个表头单元格都添加了一个可调整宽度的元素`column-resizer`,当你点击并拖动该元素,可以调整相应列的宽度。你可以根据需要修改其样式和事件处理程序来满足你的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值