本文實例介紹了javascript實現input file上傳圖片預覽效果的詳細程式碼,分享給大家供大家參考,具體內容如下
運行效果圖:
具體實現代碼:
<!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 class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div><div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox"> <div class="imgnum"> <input type="file" class="filepath" /> <span class="close">X</span> <img class="img1 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img2 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </div> </div> <div class="imgbox1"> <div class="imgnum"> <input type="file" class="filepath1" /> <span class="close1">X</span> <img class="img11 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" > <img class="img22 lazy" src="/static/imghwm/default1.png" data-src="" class=" / alt="javascript實作input file上傳圖片預覽效果" > </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 class="img11 lazy" src="/static/imghwm/default1.png" data-src="btn.png" / alt="javascript實作input file上傳圖片預覽效果" >'+ '<img class="img22 lazy" src="/static/imghwm/default1.png" data-src="'+srcs+'" / alt="javascript實作input file上傳圖片預覽效果" >'+ '</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>希望本文對大家學習javascript程式設計有幫助。 更多javascript實現input file上傳圖片預覽效果相關文章請關注PHP中文網! 🎜

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。