定义input type=file 样式的方法,使用一个text和一个button模拟

1.原理

基于安全性考虑,IE不支持用程序设置input type=file的值。

所以不能直接用<input type="text"/>和<input type="button"/>代替,

CSS美化file组件的方法是使用position属性将file组件置于<input type="text"/>和<input type="button"/>之上,

然后使其全透明化,操作时使用户感觉在操作<input type="text"/>和<input type="button"/>,实际上还是在操作file组件。 

2.源代码

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>定义input type="file" 的样式</title>  
  6. <style type="text/css">  
  7. .type-file-box {      
  8.  <span style="color: #ff0000;">position:absolute;</span>   
  9.  width:260px  
  10. }  
  11.   
  12. input {      
  13. vertical-align:middle;      
  14. margin:0;      
  15. padding:0  
  16. }  
  17.   
  18. .type-file-text {      
  19. height:22px;      
  20. border:1px solid #cdcdcd;      
  21. width:180px;  
  22. }  
  23.   
  24. .type-file-button {      
  25. background-color:#FFF;      
  26. border:1px solid #CDCDCD;      
  27. height:24px;      
  28. width:70px;  
  29. }  
  30.   
  31. .type-file-file {      
  32. <span style="color: #ff0000;">position:absolute;</span>      
  33. top:0;      
  34. right:0;      
  35. height:24px;  
  36. filter:alpha(opacity:0);          
  37. width:260px  
  38. }  
  39. </style>  
  40. </head>  
  41. <body>  
  42. <div class="type-file-box">  
  43. <form action="" method="post" name="form1" id="form1">  
  44. <input type='text' name='textfield' id='textfield' class='type-file-text' />  
  45. <input type='button' name='button' id='button' value='浏览...' class='type-file-button' />  
  46. <input name="fileField" type="file" class="type-file-file" id="fileField" size="28" onchange="javascript:document.getElementById('textfield').value=this.value;"/>  
  47. </form>  
  48. </div>  
  49. </body>  
  50. </html>  

3.注释

 外层DIV的position属性不能省略。因为

 1)absolute :

生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。

元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定

 2)static :

默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。

 

4.参考资料

http://www.jb51.net/web/39559.html

 

http://www.w3school.com.cn/css/pr_class_position.asp






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值