JS 头像显示

核能气质少年 提交于 2019-11-26 11:59:55

HTML

<div class="form-group">
    <label class="col-sm-3 control-label">头像</label>
    <div class="col-sm-9">
        <label for="avatar-id"><img src="{% static 'img/default.png' %}" alt="" id="avatar-img"></label>
        <input type="file" id="avatar-id" class="hidden">

    </div>
</div>

JS

    // 1.input file 上传标签绑定change事件
    $("#avatar-id").change(function () {
        // 2.创建一个读取文件的对象
        var fileReader = new FileReader()
        // 3.读取选择的对象
        fileReader.readAsDataURL($(this)[0].files[0])
        // 注意:文件的读取需要事件
        fileReader.onload = function () {
            // 4.读取完文件后,修改img标签的src属性
            $("#avatar-img").attr("src", fileReader.result)
        }
    });

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!