当前位置:网站首页>JS realizes clicking avatar to upload picture modification
JS realizes clicking avatar to upload picture modification
2022-04-22 07:15:00 【sunshine641】
Post only key code parts :
input Set the style of to hide , There will be no button to select a file
; event handlefile To handle uploaded images
img Click the event according to its id binding input Click events for , In this way, click the picture to click input Select Picture ; :src Here is the dynamic loading path , Unnecessary direct src=“” Just fine
<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 Code : ( I use vue Written , Only the front-end part ; To upload to the backend , You need to add the code to the database )
methods:{
// Open picture upload
uploadHeadImg: function () {
this.$el.querySelector('.hiddenInput').click() // binding input The name of the class
},
// Show Avatar
handleFile: function (e) {
let $target = e.target || e.srcElement
let file = $target.files[0]
var reader = new FileReader() // Create a file reader instance
reader.onload = (data) => {
let res = data.target || data.srcElement
this.avatar = res.result
}
reader.readAsDataURL(file)
},
}
版权声明
本文为[sunshine641]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220609132677.html
边栏推荐
- 【SVN】Subversion安装使用笔记
- 6. What is ROS
- 【Bug小记】antd表格高度自适应窗口高度
- A solution to the error of selecting objects in pyautocad
- STM32 timer ocref output configuration timx - > CCER
- mui.plusReady 不生效
- Scope and lifetime (Mr. Weng Kai)
- Mcs-5 interrupt technology (Theory)
- Introduction to IC Analog Layout - learning notes on layout Basics (5)
- 分布式任务调度与计算框架:PowerJob 简介 01
猜你喜欢

用大写的字段接受最终首字母却变小写

Relationship between Nacos namespace grouping and dataid

Fields in uppercase accept the final initial but become lowercase

任选一小说网站,爬取任意一部小说,以记事本的形式保存。

.NET学习笔记----关于.NET Core那些事(3)【配置文件的读取、json文件的通用解析、读取静态文件】

Chip design how to prepare for the upcoming autumn recruitment and spring recruitment in the coming year?

Mcs-5 interrupt technology (practice)

Design of second-order RC low-pass filter for PWM wave to DC

Introduction to IC Analog Layout - learning notes on layout Basics (4)

mysql的zip安装教程
随机推荐
First order digital low-pass filter - C language / Matlab implementation
short circuit
Review of the sixth edition of introduction to software engineering (notes)
Random库的8个函数
旋转选择器 WheelPicker 的使用
Oracle JDK vs OpenJDK
STM32 timer synchronization trigger code experimental verification and sharing
Leetcode punch in
微信小程序 canvas 画简单的仪表盘 渐近色
实验室安全考试
老程序员常逛的网站(持续更新)
Statistical categories in 911 from 2015 to 2017
leetcode打卡
pyftpdlib中文乱码问题解决方案
Service configuration center of Nacos
Use of Tencent cloud object storage service
NOI / 1.5.25:求特殊自然数
How to become an IC Verification Engineer?
Eight functions of random library
Digital IC design and CS?