js代码 <script> $(document).on("click", "#btn", function () { $('.file-upload').click(); $(document).on("change", ".file-upload", function () { var fileObj = $(".file-upload")[0]; var img = document.getElementById('btn'); var reader = new FileReader(); reader.onload = function(e) { if (reader.readyState === 2) { img.src = e.target.result; console.log(img.src); } } reader.readAsDataURL(fileObj.files[0]); }); });
</script>
html 代码
<div class="pt-4 pb-5"> <img src="/static/images/add_img.jpeg" class="img-fluid img-center" style="height: 150px; alt="" id="btn"/> <input class="file-upload" style="display:none;" type="file" accept="image/*" name="picture"/> </div>