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)
        }
    });

 

相关文章:

  • 2022-12-23
  • 2021-10-08
  • 2021-08-07
  • 2021-07-07
  • 2021-12-06
  • 2021-09-17
  • 2021-06-10
  • 2021-10-02
猜你喜欢
  • 2021-08-04
  • 2022-12-23
  • 2022-01-15
  • 2021-10-11
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案