当前位置:网站首页>JS实现点击头像上传图片修改
JS实现点击头像上传图片修改
2022-04-22 06:11:00 【sunshine641】
只贴关键代码部分:
input的样式设为隐藏 ,就不会出现选择文件的按钮
;事件handlefile来处理上传图片
img 点击事件根据其id绑定input的点击事件 ,这样点击图片即点击input选择图片 ; :src这里是在动态加载路径 ,不需要的直接 src=“” 就好
<input type="file" id="f" accept="image/*" @change="handleFile" class="hiddenInput" style="display:none"/>
<img id="img" class="avatarImg" :src="this.avatar" onclick="f.click()">
JS代码: (我这里是用vue写的,只实现前端部分;要上传到后端,需要再加传到数据库的代码 )
methods:{
// 打开图片上传
uploadHeadImg: function () {
this.$el.querySelector('.hiddenInput').click() //绑定input的类名
},
// 将头像显示
handleFile: function (e) {
let $target = e.target || e.srcElement
let file = $target.files[0]
var reader = new FileReader() //创建文件阅读器实例
reader.onload = (data) => {
let res = data.target || data.srcElement
this.avatar = res.result
}
reader.readAsDataURL(file)
},
}
版权声明
本文为[sunshine641]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sunshine641/article/details/110533043
边栏推荐
- Preparation before analog circuit board commissioning_ Analog circuit board
- New year's greetings & wishes to all programmers
- SeekBar 自定义 样式 详解
- Distributed task scheduling and computing framework: introduction to powerjob 01
- Start with stm32f4 floating point operation (FPU) function + use DSP Library
- Alternative ps8625 | alternative ps8622 | design and development of cost-effective EDP to LVDS adapter board cs5211
- Matlab: female voice to male voice
- 旋转选择器 WheelPicker 的使用
- Leetcode 1218: Longest definite difference subsequence
- 泛型与反射的实际使用练习(包含一个泛型缓存)----手写ORM框架
猜你喜欢

Nacos service consumer registration and load balancing

Goodbye, postman. One thing to say: apifox is yyds

MATLAB:去除音频信号噪音

Nacos cluster architecture

From spec. to chip_ (Digital IC, analog IC, FPGA / CPLD design process and EDA tools)

Difference between analog IC design and digital IC design, including salary table

如何成为IC验证工程师?

STM32 learning record - development environment installation

Does microelectronics major make chips? What is the chip related to?

Alternative ps8625 | alternative ps8622 | design and development of cost-effective EDP to LVDS adapter board cs5211
随机推荐
Application of usbcan card in CAN bus in EOL test system of power battery pack
Difference between analog IC design and digital IC design, including salary table
[review of Blue Bridge Cup] tree of life
Nacos命名空间分组和DataID三者关系
.NET学习笔记----关于.NET Core那些事(1)【.netcore的项目结构、五种向页面传值的方式、Log4Net和NLog的使用】
Notes on C # daily development ----- obtain all files in the zip in Huawei cloud bucket (including system. Notsupportedexception: "this stream does not support search operation" solution)
Can bus record diagnostic assistant
阿里云部署RSSHub踩坑笔记
【Bug小记】页面刷新后获取不到sessionstorage数据
STM32 learning record 0002-stm32
leetcode 打卡
集成电路模拟版图入门-版图基础学习笔记(三)
STM32 timer synchronization trigger code experimental verification and sharing
从FAB厂转到IC验证的心酸历程
Statistical categories in 911 from 2015 to 2017
STM32 learning record - development environment installation
What knowledge and skills need to be added to IC design?
芯片设计怎样准备即将面临的秋季补招和来年的春招?
【Bug小记】keepalive下mounted只执行一次
Introduction to IC Analog Layout - learning notes on layout Basics (4)