前端H5,點擊選擇圖片控件,圖片直接在頁面上展示~


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="upload_box">
    <b>上傳圖片</b>
    <input type="file" name="file" id="file" accept="image/*" onchange="imgChange(this);"/> <!--文件上傳選擇按鈕-->
    <div id="preview">
        <img id="imghead" src=""/> <!--圖片顯示位置-->
    </div>
</div>

</body>
</html>
<script type="text/javascript">
    // 選擇圖片顯示
    function imgChange(obj) {
//獲取點擊的文本框
        var file = document.getElementById("file");
        var imgUrl = window.URL.createObjectURL(file.files[0]);
        var img = document.getElementById('imghead');
        img.setAttribute('src', imgUrl); // 修改img標簽src屬性值
    };
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM