只是想改变风格:
<input type="file" value="Choose the file" />
看起来像:
<a href="#">Choose the file</a>
只使用CSS,是否可能实现这个效果?
只是想改变风格:
<input type="file" value="Choose the file" />
看起来像:
<a href="#">Choose the file</a>
只使用CSS,是否可能实现这个效果?
input[type="file"]
设置样式,而这个解决方案在我看来是最好的。 - Vucko可能需要对大小、悬停状态等进行微调,但为什么不这样做呢:
span {
cursor: pointer;
}
a {
position: relative;
overflow: hidden;
}
span:hover a {
color: red;
}
a + input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
}
<span><a href='#'>Link!</a><input type='file' /></span>
input type=file
定位在链接上方并将其不透明度设置为零,以便它仍然可以捕获正常的用户交互行为。a
标签内放置一个input
是无效的。 - putvandeopacity
设置为0,并使其成为position:absolute
,以便它不影响页面。你可以隐藏它,但我认为一些浏览器将不会触发标签功能,因此你将无法选择文件。a
标签中,这样它就会像页面上的其他链接一样运作。$('input[type="file"]').change(function() {
// find the label for the currrent file input
$('label[for="' + this.id + '"]').text('Choose the file - ' + $(this).val());
});
input[type=file] {
position: absolute;
top: 0;
right: 0;
filter: alpha(opacity=0);
opacity: 0;
margin: 0;
padding: 0;
}
a > label {
/* inherit the hand cursor from the a tag */
cursor:inherit;
}
a:hover {
color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="#">
<input id="thefile" type="file" value="Choose the file" />
<a href="#"><label for="thefile">Choose the file</label></a>
</form>
input {
opacity: 0;
position: relative;
left: -100px
}
input
标签放在 a
标签内是无效的。 - putvande这里有另一种类似于上面的解决方案,但我将输入标签分开并显示链接。
div.fileinputs {
position: relative;
}
div.fakefile {
position: absolute;
top: 0px;
left: 0px;
z-index: 1;
}
input.file {
position: relative;
text-align: right;
-moz-opacity:0 ;
filter:alpha(opacity: 0);
opacity: 0;
z-index: 2;
}
还有HTML
<div class="fileinputs">
<input type="file" class="file" />
<div class="fakefile">
<a href="">browse file</a>
</div>
</div>
这里是fiddle。