我想从Google Chrome中的文件输入中删除“未选择文件”工具提示(我发现Firefox中没有显示工具提示)。
请注意,我所说的不是输入字段中的文本,而是关于将鼠标移到输入上时出现的工具提示。
我试过这个没有运气:
$('#myFileInput').attr('title', '');
这是webkit浏览器的本机部分,您无法将其删除。您应该考虑像覆盖或隐藏文件输入这样的hacky解决方案。
一个hacky解决方案:
input[type='file'] {
opacity:0
}
<div>
<input type='file'/>
<span id='val'></span>
<span id='button'>Select File</span>
</div>
$('#button').click(function(){
$("input[type='file']").trigger('click');
})
$("input[type='file']").change(function(){
$('#val').text(this.value.replace(/C:\\fakepath\\/i, ''))
})
这里的所有答案都完全过于复杂,或者完全错误。
HTML:
<div>
<input type="file" />
<button>Select File</button>
</div>
CSS:
input {
display: none;
}
JavaScript的:
$('button').on('click', function(){
$('input').trigger('click');
});
我以最简单的方式创造了这个小提琴。单击“选择文件”按钮将显示文件选择菜单。然后,您可以按照自己想要的方式设置按钮的样式。基本上,您需要做的就是隐藏文件输入,并在单击另一个按钮时触发合成单击。我在IE 9,FF和Chrome上进行了测试,它们都运行良好。
为了达到这个目的,您需要自定义控件。
包裹并隐形。适用于Chrome,Safari和FF。
label {
padding: 5px;
background: silver;
}
label > input[type=file] {
display: none;
}
<label>
<input type="file">
select file
</label>
直接你不能修改输入[type = file]。
使输入类型文件不透明度为0并尝试使用自定义CSS在其上放置一个相对元素[div / span / button]
最好避免使用不必要的JavaScript。您可以利用label标签扩展输入的点击目标,如下所示:
<label>
<input type="file" style="display: none;">
<a>Open</a>
</label>
即使输入被隐藏,链接仍然可以作为它的点击目标,您可以根据需要设置它的样式。
即使您将不透明度设置为零,也会出现工具提示。在元素上尝试visibility:hidden
。它对我有用。
您可以为元素设置宽度,该宽度仅显示按钮并隐藏“无文件选择”。
我为此寻找好的答案......我发现了这个:
首先删除'没有选择的文件'
input[type="file"]{
font-size: 0px;
}
然后使用-webkit-file-upload-button
工作按钮,这样:
input[type="file"]::-webkit-file-input-button{
font-size: 16px; /*normal size*/
}
希望这是你想要的,它对我有用。
结合上面的一些建议,使用jQuery,这就是我做的:
input[type='file'].unused {
color: transparent;
}
和:
$(function() {
$("input[type='file'].unused").click( function() {$(this).removeClass('unused')});
};
并将类“未使用”放在文件输入上。这很简单,效果很好。
对我来说,我只是希望文本不可见,仍然使用本机浏览器按钮。
input[type='file'] {
color: transparent;
}
我喜欢所有未定义的建议,但我有一个不同的用例,希望这可以帮助处于相同情况的人。
对我来说,最好的解决方案是将输入[type =“file”]包装在一个包装器中,并添加一些jquery代码:
$(function(){
function readURL(input){
if (input.files && input.files[0]){
var reader = new FileReader();
reader.onload = function (e){
$('#uploadImage').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#image").change(function(){
readURL(this);
});
});
#image{
position: absolute;
top: 0;
left: 0;
opacity: 0;
width: 75px;
height: 35px;
}
#uploadImage{
position: relative;
top: 30px;
left: 70px;
}
.button{
position: relative;
width: 75px;
height: 35px;
border: 1px solid #000;
border-radius: 5px;
font-size: 1.5em;
text-align: center;
line-height: 34px;
}
<form action="#" method="post" id="form" >
<div class="button">
Upload<input type="file" id="image" />
</div>
<img id="uploadImage" src="#" alt="your image" width="350" height="300" />
</form>
我提出了一个hacky解决方案,完全删除了“没有选择文件”以及在该文本之后添加的额外空间(在Chrome中我得到类似:“没有选择文件”)。
这完全弄乱了我的页面对齐,所以我真的和它一起努力寻找解决方案。在输入标记的样式属性中,将“宽度”设置为按钮的宽度将消除尾随文本和空格。由于按钮的宽度在所有浏览器中都不相同(例如,在Firefox中稍微小一点),您还需要将样式的颜色设置为与页面背景相同的颜色(否则为“迷路”)没有“可以透露”。我的输入文件标签如下所示:
<input style="float:left; **width:88px;** **color:#000000;**" type="file" id="fileInput" onclick="fileOpen()">
这个对我有用!
input[type="file"]{
font-size: 0px;
}
然后,您可以使用不同类型的样式,如width
,height
或其他属性,以创建自己的输入文件。
很惊讶没有人提到有关event.preventDefault()
的事
$("input[type=file]").mouseover(function(event) {
event.preventDefault();
// This will disable the default behavior of browser
});
可以使用title属性编辑默认工具提示
<input type='file' title="your text" />
但是,如果您尝试删除此工具提示
<input type='file' title=""/>
这不行。这是我的小技巧,用空格尝试标题。它会工作。:)
<input type='file' title=" "/>
您可以禁用工具提示,在Chromekit等Webkit浏览器上设置空格,在Firefox或IE上设置空字符串(在Chrome 35,FF 29,IE 11,safari mobile上测试)
$('input[type="file"]').attr('title', window.webkitURL ? ' ' : '');
这个适用于我(至少在Chrome和Firefox中):
<input type="file" accept="image/*" title=" "/>
很简单,忘记了针对输入[“类型”]的CSS,它对我不起作用。我不知道为什么。我的HTML标签中有我的解决方案
<input type="file" style="color:transparent; width:70px;"/>
问题结束了
我找到了一个解决方案,只需在title属性中设置一个空格即可。
<input type="file" value="" title=" " />
这是一个棘手的问题。我找不到选择'no file selected'元素的方法,所以我使用:after伪选择器创建了一个掩码。
我的解决方案还需要使用以下伪选择器来设置按钮的样式:
::-webkit-file-upload-button
试试这个:http://jsfiddle.net/J8Wfx/1/
仅供参考:这只适用于webkit浏览器。
如果有人知道如何在webkit检查器中查看webkit伪选择器,请告诉我
在所有浏览器和简单。这对我来说
$(function () {
$('input[type="file"]').change(function () {
if ($(this).val() != "") {
$(this).css('color', '#333');
}else{
$(this).css('color', 'transparent');
}
});
})
input[type="file"]{
color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="app_cvupload" class="fullwidth input rqd">