javascript實(shí)現(xiàn)input file上傳圖片預(yù)覽效果
本文實(shí)例介紹了javascript實(shí)現(xiàn)input file上傳圖片預(yù)覽效果的詳細(xì)代碼,分享給大家供大家參考,具體內(nèi)容如下
運(yùn)行效果圖:

具體實(shí)現(xiàn)代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<style type="text/css">
.imgbox,.imgbox1
{
float: left;
margin-right: 20px;
margin-top: 20px;
position: relative;
width: 182px;
height: 142px;
border: 1px solid red;
overflow: hidden;
}
.imgbox1{border: 1px solid blue;
}
.imgnum{
left: 0px;
top: 0px;
margin: 0px;
padding: 0px;
}
.imgnum input,.imgnum1 input {
position: absolute;
width: 182px;
height: 142px;
opacity: 0;
}
.imgnum img,.imgnum1 img {
width: 100%;
height: 100%;
}
.close,
.close1 {
color: red;
position: absolute;
left: 170px;
top: 0px;
display: none;
}
</style>
</head>
<body>
<div id="img">
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div><div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div> <div class="imgbox">
<div class="imgnum">
<input type="file" class="filepath" />
<span class="close">X</span>
<img src="btn.png" class="img1" />
<img src="" class="img2" />
</div>
</div>
<div class="imgbox1">
<div class="imgnum">
<input type="file" class="filepath1" />
<span class="close1">X</span>
<img src="btn.png" class="img11" />
<img src="" class="img22" />
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(function() {
$(".filepath").on("change",function() {
alert($('.imgbox').length);
var srcs = getObjectURL(this.files[0]); //獲取路徑
$(this).nextAll(".img1").hide(); //this指的是input
$(this).nextAll(".img2").show(); //fireBUg查看第二次換圖片不起做用
$(this).nextAll('.close').show(); //this指的是input
$(this).nextAll(".img2").attr("src",srcs); //this指的是input
$(this).val(''); //必須制空
$(".close").on("click",function() {
$(this).hide(); //this指的是span
$(this).nextAll(".img2").hide();
$(this).nextAll(".img1").show();
})
})
})
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) {
url = window.createObjectURL(file)
} else if (window.URL != undefined) {
url = window.URL.createObjectURL(file)
} else if (window.webkitURL != undefined) {
url = window.webkitURL.createObjectURL(file)
}
return url
};
$(function() {
$("#img").on("change",".filepath1",function() {
//alert($('.imgbox1').length);
var srcs = getObjectURL(this.files[0]); //獲取路徑
alert(srcs);
//this指的是input
/* $(this).nextAll(".img22").attr("src",srcs); //this指的是input
$(this).nextAll(".img22").show(); //fireBUg查看第二次換圖片不起做用*/
var htmlImg='<div class="imgbox1">'+
'<div class="imgnum1">'+
'<input type="file" class="filepath1" />'+
'<span class="close1">X</span>'+
'<img src="btn.png" class="img11" />'+
'<img src="'+srcs+'" class="img22" />'+
'</div>'+
'</div>';
$(this).parent().parent().before(htmlImg);
$(this).val(''); //必須制空
$(this).parent().parent().prev().find(".img11").hide(); //this指的是input
$(this).parent().parent().prev().find('.close1').show();
$(".close1").on("click",function() {
$(this).hide(); //this指的是span
$(this).nextAll(".img22").hide();
$(this).nextAll(".img11").show();
if($('.imgbox1').length>1){
$(this).parent().parent().remove();
}
})
})
})
</script>
</html>
希望本文所述對(duì)大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。
- JS使用H5實(shí)現(xiàn)圖片預(yù)覽功能
- 簡(jiǎn)單實(shí)現(xiàn)JS上傳圖片預(yù)覽功能
- javascript圖片預(yù)覽和上傳(兼容IE)
- angularjs點(diǎn)擊圖片放大實(shí)現(xiàn)上傳圖片預(yù)覽
- JS HTML5拖拽上傳圖片預(yù)覽
- js實(shí)現(xiàn)純前端的圖片預(yù)覽
- 輕松實(shí)現(xiàn)js圖片預(yù)覽功能
- js實(shí)現(xiàn)上傳圖片預(yù)覽的方法
- 上傳圖片預(yù)覽JS腳本 Input file圖片預(yù)覽的實(shí)現(xiàn)示例
- viewer.js實(shí)現(xiàn)圖片預(yù)覽功能
相關(guān)文章
javascript 關(guān)于# 和 void的區(qū)別分析
href 為#的跳到了自己的頁(yè)面。原來(lái)‘#’代表的是 #top ,2009-10-10
一文帶你掌握J(rèn)avaScript中Moment.js如何操作日期和時(shí)間
Moment.js是一個(gè)極其強(qiáng)大的JavaScript庫(kù),專(zhuān)門(mén)用于解析、驗(yàn)證、操作和顯示日期和時(shí)間,下面就跟隨小編一起學(xué)習(xí)一下Moment.js的具體使用吧2024-01-01
JavaScript創(chuàng)建對(duì)象的幾種方式小結(jié)
這篇文章主要介紹了 JavaScript 中創(chuàng)建對(duì)象的多種方式,包括字面量方式、構(gòu)造函數(shù)方式、原型模式、() 方法、ES6 類(lèi),并分別說(shuō)明了它們的特點(diǎn)和適用場(chǎng)景,強(qiáng)調(diào)在實(shí)際編程中要根據(jù)需求選擇合適的方式以提高代碼質(zhì)量和性能,需要的朋友可以參考下2024-12-12
JavaScript如何使用Promise實(shí)現(xiàn)分批處理接口請(qǐng)求
當(dāng)我們?cè)陂_(kāi)發(fā)時(shí)遇到需要同時(shí)發(fā)起百條接口請(qǐng)求怎么辦呢,本文主要來(lái)和大家介紹一下JavaScript如何使用Promise實(shí)現(xiàn)分批處理接口請(qǐng)求,需要的可以參考下2024-03-03
Javascript函數(shù)緩存的實(shí)現(xiàn)及應(yīng)用場(chǎng)景
Javascript函數(shù)緩存是一種提高網(wǎng)頁(yè)性能的重要技術(shù),通過(guò)將函數(shù)結(jié)果存儲(chǔ)在緩存中,避免重復(fù)計(jì)算,從而提高頁(yè)面加載速度和響應(yīng)速度,本文主要介紹了Javascript函數(shù)緩存的實(shí)現(xiàn)及應(yīng)用場(chǎng)景,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12

