jq添加<img>点击事件调用上传图片方法并回显图片

效果图

在这里插入图片描述

1.html添加如下元素

<img src="https://img1.baidu.com/it/u=1983462487,1113805563&fm=26&fmt=auto" alt="" width="80" id="logoImg" onclick="imgChange()">
<input type="file" style="width: 305px;display: none" name="logoPicUrl" multiple="multiple" id="logoPicUrl" autocomplete="off" 
accept="image/x-png,image/gif,image/jpeg,image/bmp">

2.js添加如下方法

/**
 * 点击图片事件
 */
function imgChange() {
    $('#logoPicUrl').click();
    $("#logoPicUrl").unbind().change(function(e) {
        if ($('#logoPicUrl').val() == '') {
            return;
        }
        changeImg(e,$(this).val());
    });
}

/**
 * 改变图片
 */
function changeImg(e,filePath) {
    fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    //检查后缀名
    if (!fileFormat.match(/.png|.jpg|.jpeg/)) {
        showError('文件格式必须为:png/jpg/jpeg');
        return;
    }
    //获取并记录图片的base64编码
    var reader = new FileReader();
    reader.readAsDataURL(e.target.files[0]);
    reader.onloadend = function() {
        // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
        var dataURL = reader.result;
        console.log(dataURL)
        // 显示图片
        $("#logoImg").attr('src',dataURL);
    };
}

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>点击图片上传并回显</title>
	<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
	<style>
		
	</style>
</head>

<body>
	<img src="https://img1.baidu.com/it/u=1983462487,1113805563&fm=26&fmt=auto" alt="" width="80" id="logoImg" onclick="imgChange()">
	<input type="file" style="width: 305px;display: none" name="logoPicUrl" multiple="multiple" id="logoPicUrl" autocomplete="off" 
	accept="image/x-png,image/gif,image/jpeg,image/bmp">
</body>
<script>
/**
 * 点击图片事件
 */
function imgChange() {
    $('#logoPicUrl').click();
    $("#logoPicUrl").unbind().change(function(e) {
        if ($('#logoPicUrl').val() == '') {
            return;
        }
        changeImg(e,$(this).val());
    });
}

/**
 * 改变图片
 */
function changeImg(e,filePath) {
    fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
    //检查后缀名
    if (!fileFormat.match(/.png|.jpg|.jpeg/)) {
        showError('文件格式必须为:png/jpg/jpeg');
        return;
    }
    //获取并记录图片的base64编码
    var reader = new FileReader();
    reader.readAsDataURL(e.target.files[0]);
    reader.onloadend = function() {
        // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
        var dataURL = reader.result;
        console.log(dataURL)
        // 显示图片
        $("#logoImg").attr('src',dataURL);
    };
}

</script>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哎呦喂O_o嗨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值