對(duì)input按鈕樣式的美化,思路有兩個(gè):
- 1.將input元素的opacity設(shè)置為0,外面用div包裹。視覺上看不到input元素,但點(diǎn)擊動(dòng)作還是在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
,然后將點(diǎn)擊其余DOM時(shí),觸發(fā)input元素的click事件即可。那個(gè)顯性顯示的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>