本来在一般js中操作dom以如下方式实现:
<input type="file" name="" id="f" value="" />
<script type="text/javascript">
document.getElementById("f").onchange=function(){
this.value=""
}
</script>
在vue3中采用 ref 获取dom元素
<input type="file" id="touxiang" ref="photo" @change="change" accept="image/*" >
//js部分
function change(){
if(photo.value.files[0].size<209927){
user.user_photo=photo.value.files[0].name
}else{
photo.value=''
alert('图片大小超过209927,请重新选择')
}
}
图片超出大小还是会选中,思考之后改正:
function change(){
if(photo.value.files[0].size<209927){
user.user_photo=photo.value.files[0].name
}else{
photo.value.value='' //修改了此处
alert('图片大小超过209927,请重新选择')
}
}