JS实现图片上传预览效果

<form>

<img ID="UserImg" /><br/>

<input type="file" name="fileimage" id="fileimage" style="display: none;"  />

<br />

<input id="Btn_UserImg" type="button" value="选择图片" />

<script>

function Urlimg(e) {

var url = null;

if (window.URL != null) {

url = window.URL.createObjectURL(e);

} else if (window.createObjectURL != null) {

url = window.createObjectURL(e);

} else if (window.webkitURL != null) {

url = window.webkitURL.createObjectURL(e);

}

return url;

};

document.getElementById("Btn_UserImg").onclick = function() {

document.getElementById("fileimage").click();

document.getElementById("fileimage").onchange = function() {

var img = Urlimg(document.getElementById("fileimage").files[0]);

document.getElementById("UserImg").setAttribute("src", img);

};

};

</script>

</form>

上一篇:VSCode如何与ESLint和prettier结合使用


下一篇:Java学习 day04_JS&DOM