对input按钮样式的美化,思路有两个:
- 1.将input元素的opacity设置为0,外面用div包裹。视觉上看不到input元素,但点击动作还是在input上
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
position: relative;
width: 200px;
height: 50px;
line-height: 50px;
background-color: pink;
color: #fff;
text-align: center;
}
.input{
position: absolute;
width: 100%;
height: 100%;
top: 0;
right: 0;
opacity: 0;
}
</style>
</head>
<body>
<div class="box" id="uploadBox">
<span>选择文件</span>
<input class="input" id="uploadInput" type="file" multiple onchange="upload"/>
</div>
</body>
</html>
- 2.直接将input元素隐藏:
display:none
,然后将点击其余DOM时,触发input元素的click事件即可。那个显性显示的DOM元素的样式爱怎么写怎么写。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{
width: 200px;
height: 50px;
line-height: 50px;
background-color: pink;
color: #fff;
text-align: center;
}
.input{
display: none;
}
</style>
</head>
<body>
<div class="box" id="uploadBox">
<span>选择文件</span>
<input class="input" id="uploadInput" type="file" multiple onchange="upload"/>
</div>
<script>
var domUploadBox = document.getElementById('uploadBox')
var domUploadInput = document.getElementById('uploadInput')
domUploadBox.addEventListener('click', refsInput)
function refsInput () {
domUploadInput.click();
}
</script>
</body>
</html>