其他分享
首页 > 其他分享> > JS实现图片上传预览效果

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>

标签:null,预览,url,getElementById,JS,window,createObjectURL,document,上传
来源: https://www.cnblogs.com/LanHai12/p/15258188.html