简体   繁体   English

使用jQuery-UI的文件输入按钮样式

[英]File Input Button Style with jQuery-UI

I'm using jQuery UI and I notice that the Input File button: 我正在使用jQuery UI,我注意到输入文件按钮:

<input type="file">

can't be styled like other buttons. 不能像其他按钮一样设计。 I found this plugin which looks cool and apparently allows you to put an image instead of the button, but the examples don't show any button styling. 我发现这个插件看起来很酷,显然可以让你放一个图像而不是按钮,但是这些例子没有显示任何按钮样式。

If there's no jQuery to apply the theme style to that button, can I style it myself in CSS? 如果没有jQuery将主题样式应用于该按钮,我可以在CSS中自己设置样式吗?

Yes you can, with only CSS. 是的,你可以,只有CSS。

Make a <form> , wrap a <div> inside it & put your <input type="file"> in the <div> . 创建一个<form> ,在其中包装一个<div>并将<input type="file">放在<div>

To override the default styling of the <input type="file"> , the main priority is to set the opacity: 0; 要覆盖<input type="file">的默认样式,主要优先级是设置opacity: 0; on the input so that the styling comes from the <div> css. 在输入上,以便样式来自<div> css。


UPDATE: 更新:

To get the text from the <input type="file"> you need a bit of jQuery. 要从<input type="file">获取文本,您需要一些jQuery。

For example, add a paragraph. 例如,添加一个段落。 Then get the text from the input using val() and set it as the paragraphs text. 然后使用val()从输入中获取文本并将其设置为段落文本。 Updated Fiddle: 更新小提琴:


Fiddle demo 小提琴演示

A simple solution: 简单的解决方案:

Markup: 标记:

<div class="fileUpload btn btn-primary">
    <span>Upload</span>
    <input type="file" class="upload" />
</div>

Css: CSS:

.fileUpload {
    position: relative;
    overflow: hidden;
    margin: 10px;
}
.fileUpload input.upload {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 20px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
}

Demo 演示

Note: I have used Bootstrap for styling button ( div.file-upload ). 注意:我使用Bootstrap作为样式按钮( div.file-upload )。

May be you want something like this: 可能你想要这样的东西:

Html: HTML:

<div id='file'>
   <label id='text'for="inputFile1">No file selected.</label>
   <label for="inputFile1">
       <input type="file" id='inputFile1' class='fileInput'/>
   </label>
</div>

css: CSS:

input[type="file"] {
    opacity:0;
}
#file {
    background:url('...der_open-add.png') 0 0 no-repeat;
    width:100%;
    height:32px;
    position:relative;
    overflow:hidden;
}
#file label {
    padding:0 0 0 35px;
    color:green;
    width:100%;
}
#file #text {
    line-height:32px;
    width:100%;
    position:absolute;
    left:0px;
    top:0;
    color:green;
    font-size:11px;
}

and jQuery: 和jQuery:

$('#file input[type="file"]').on('change', function () {
    var o = this.value || 'No file selected.';
    $(this).closest('#file').find('#text').text(o);
});

Fiddle 小提琴

<div style="position:relative;display:inline-block;left:-4px;bottom:-6px;width:16px;  height: 24px;overflow:hidden;">
<img src="/images/attach.jpg" alt="" title="Add Attachment" style="height:24px;width:16px; position: relative;top: 1px; left: 0px;"/>
<input type="file" id="fileupload" name="upload" style=" opacity: 0;font-size: 50px;width:16px; filter:alpha(opacity: 0);  position: relative; top: -22px; left: -1px" />
</div>

Demo: 演示:

http://jsfiddle.net/k6zBQ/2/ http://jsfiddle.net/k6zBQ/2/

My simple solution: Put the input button in a div with css display:none Make your button anyway you want and bind a function that trigger the invisible input button. 我的简单解决方案:将输入按钮放在带有css display的div中:none无论如何都要按下你的按钮并绑定一个触发隐藏输入按钮的功能。

<div style="display: none;">
    <form id="fileupload">
        <input type="file" class="upload" name="newfile"/>
    </form>
</div>

<span id="browser">Click this line to browse all your awesome files</span>

JQUERY:
$("#browser").on('click', function(){
    $(".upload").click();       
});

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM