上传图片,并替换图片
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
img{
width: 200px;
height: 300px;
}
</style>
</head>
<body>
<img src="" >
<input class="fileinp" type="file" name="" id="" value="" />
</body>
</html>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
$(function(){
$(".fileinp").change(function() {
var reader = new FileReader();
//将上传的图片地址转化为data开头的
reader.readAsDataURL(this.files[0]);
reader.onload = function(e) {
console.log(e.target.result);
if (e.target.result !== "") {
// 替换图片地址
$("img").attr("src",e.target.result)
}
}
});
})
</script>