jQuery實(shí)現(xiàn)多張圖片上傳預(yù)覽(不經(jīng)過后端處理)
更新時(shí)間:2017年04月29日 10:44:36 作者:15868870855
本篇文章主要介紹了jQuery實(shí)現(xiàn)多張圖片上傳預(yù)覽(不經(jīng)過后端處理)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
效果圖:
圖(1)
圖(2)
代碼如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery圖片上傳預(yù)覽(不經(jīng)過后端處理)</title> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> <style> </style> </head> <body> <div> <img class="ImgPr"/> <input type="file" class="up" /> </div> <div> <img class="ImgPr"/> <input type="file" class="up" /> </div> <script> jQuery.fn.extend({ uploadPreview: function(opts) { var _self = this, _this = $(this); opts = jQuery.extend({ Img: "ImgPr", Width: 100, Height: 100, ImgType: ["gif", "jpeg", "jpg", "bmp", "png"], Callback: function() {} }, opts || {}); _self.getObjectURL = function(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 }; _this.change(function() { if (this.value) { if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) { alert("選擇文件錯(cuò)誤,圖片類型必須是" + opts.ImgType.join(",") + "中的一種"); this.value = ""; return false } //高版本Jquey使用 if ($.support.leadingWhitespace) if ($.support.leadingWhitespace) { //低版本jquery中使用$.browser.msie console.log(_self.getObjectURL(this.files[0])); try { _this.parent('div').find("." + opts.Img).attr('src', _self.getObjectURL(this.files[0])); } catch (e) { var src = ""; var obj = _this.parent('div').find("." + opts.Img); var div = obj.parent("div")[0]; _self.select(); if (top != self) { window.parent.document.body.focus() } else { _self.blur() } src = document.selection.createRange().text; document.selection.empty(); obj.hide(); obj.parent("div").css({ 'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)', 'width': opts.Width + 'px', 'height': opts.Height + 'px' }); div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src } } else { _this.parent('div').find("." + opts.Img).attr('src', _self.getObjectURL(this.files[0])) } opts.Callback() } }) } }); $(".up").click(function(){ $(this).uploadPreview({ Img: "ImgPr" }); }) </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
jQuery綁定事件-多種實(shí)現(xiàn)方式總結(jié)
下面小編就為大家?guī)硪黄猨Query綁定事件-多種實(shí)現(xiàn)方式總結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05jquery彈窗時(shí)禁止body滾動(dòng)條滾動(dòng)的例子
今天小編就為大家分享一篇jquery彈窗時(shí)禁止body滾動(dòng)條滾動(dòng)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09淺析jQuery中調(diào)用ajax方法時(shí)在不同瀏覽器中遇到的問題
這篇文章主要介紹了jQuery中調(diào)用ajax方法時(shí)在不同瀏覽器中遇到的問題,因不同瀏覽器默認(rèn)設(shè)置的不同造成的問題2014-06-06jquery漸隱漸顯的圖片幻燈閃爍切換實(shí)現(xiàn)方法
這篇文章主要介紹了jquery漸隱漸顯的圖片幻燈閃爍切換實(shí)現(xiàn)方法,實(shí)例分析了jQuery操作圖片的技巧及鼠標(biāo)事件用法,需要的朋友可以參考下2015-02-02jQuery仿360導(dǎo)航頁(yè)圖標(biāo)拖動(dòng)排序效果代碼分享
這篇文章主要為大家詳細(xì)介紹了360導(dǎo)航頁(yè)圖標(biāo)拖動(dòng)排序效果代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08

jquery實(shí)現(xiàn)界面無刷新加載登陸注冊(cè)
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)界面無刷新加載登陸注冊(cè),具有一定的參考價(jià)值,感興趣的朋友可以參考一下
2016-07-07