第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何顯示我輸入的圖像

如何顯示我輸入的圖像

函數式編程 2023-03-03 09:58:51
我制作了一個將圖像作為輸入的按鈕,但我找不到顯示圖片的方法。我試圖制作一個空元素,然后像我在其他網站上看到的那樣更改其 URL,但它沒有用。HTML <input      id="myImage"     class="photo-upload"     type="file"     accept="image/*, image/jpeg">     <img id="the-picture" width="200" />JavaScripts const uploadPictureButton = document.querySelector(".photo-upload"); uploadPictureButton.addEventListener("click", displayPicture); function displayPicture(event) {     let image = document.getElementById("myImage");     image.src = URL.createObjectURL(event.target.files[0]); };
查看完整描述

3 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

代碼中需要一些修復,必須讀取文件并創(chuàng)建 URL,這里是代碼的工作片段。


const uploadPictureButton = document.querySelector(".photo-upload");


  uploadPictureButton.addEventListener('change', function () {

    displayPicture(this);

  });


 function displayPicture(input) {

    if (input.files && input.files[0]) {

    var reader = new FileReader();


    reader.onload = function (e) {

      document.getElementById('the-picture').setAttribute('src', e.target.result);

    };


    reader.readAsDataURL(input.files[0]);

  }

 }

 <input 

     id="myImage"

     class="photo-upload"

     type="file"

     accept="image/*, image/jpeg">


<img id="the-picture" width="200" /> 


查看完整回答
反對 回復 2023-03-03
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

您也可以使用innerHTML來顯示圖像。



查看完整回答
反對 回復 2023-03-03
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

試試這個,它對我有用

<img height="100" width="100" alt="avatar" id="imgPreview> <input type="file" onchange="document.querySelector("#imgPreview").src = window.URL.createObjectURL(this.files[0])">


查看完整回答
反對 回復 2023-03-03
  • 3 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號