用选择文件<input type="file">
,原始样式不怎么好看,想要好看一点,就要自己写样式来代替原始的样式
//原始的样式
//自己写的样式
<label class=“mt-2”>附件途径:</label>
<div class=“custom-file”>
//选择文件原始样式
<input type=“file” id=“choicefile”>
//输入框
<input type=“text” class=“d-inline-block” id=“filename”>
//绑定选择文件的点击按钮
// “for” 属性可把 label 绑定到id为choicefile里
// for: “id”
<label class=“form-control form-control-sm text-white text-center ml-2 mt-2” style=“width:80px;height:30px;background-color:#5cb85c;cursor:
pointer;” for=“choicefile”>浏览</label>
</div>
隐藏选择文件的原始样式 hidden
onchange在元素值改变时触发,点击浏览选择文件打开,会获取到选择文件的原始样式的value值,把值给到id为’filename’的input的value
//代码
<label class="mt-2">附件途径:</label>
<div class="custom-file">
<input type="file" id="choicefile" onchange="document.getElementById('filename').value = this.value;" hidden>
<input type="text" class="form-control form-control-sm d-inline-block" style="margin-left:20px;" id="filename">
<label class="form-control form-control-sm text-white text-center ml-2 mt-2" style="width:80px;height:30px;background-color:#5cb85c;cursor: pointer;" for="choicefile">浏览</label>
</div>
没有选择到文件的
选择到文件的
点击浏览,会跳出选择文件的框,选择好了文件,打开,input 里就会显示出你选择文件的路径
如图