<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="jq路径" ></script>
<title>确认订单</title>
<style>
.pic{
width: 100px;
height: 100px;
background-image: url(图片路径);
background-size: cover;
}
</style>
</head>
<body>
<div>
<img class="pic">
<input class="upload" name="file" type="file" style="display: none"/>
</div>
<!--<div>
<img class="pic" src="" >
<input class="upload" name="file" accept="image/*" type="file" style="display: none"/>
</div>-->
</body>
</html>
<script>
$(function() {
$(".pic").click(function () {
$(this).parent().find(".upload").click(); //隐藏了input:file样式后,点击头像就可以本地上传
$(this).parent().find(".upload").on("change",function(){
var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径,该路径不是图片在本地的路径
if (objUrl) {
$(this).parent().find(".pic").attr("src", objUrl) ; //将图片路径存入src中,显示出图片
}
});
});
});
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="jq路径" ></script>
<title>确认订单</title>
<style>
.pic{
width: 100px;
height: 100px;
background-image: url(图片路径);
background-size: cover;
}
</style>
</head>
<body>
<div>
<img class="pic">
<input class="upload" name="file" type="file" style="display: none"/>
</div>
<!--<div>
<img class="pic" src="" >
<input class="upload" name="file" accept="image/*" type="file" style="display: none"/>
</div>-->
</body>
</html>
<script>
$(function() {
$(".pic").click(function () {
$(this).parent().find(".upload").click(); //隐藏了input:file样式后,点击头像就可以本地上传
$(this).parent().find(".upload").on("change",function(){
var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径,该路径不是图片在本地的路径
if (objUrl) {
$(this).parent().find(".pic").attr("src", objUrl) ; //将图片路径存入src中,显示出图片
}
});
});
});
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
</script>
</body>
</html>