JS实现多张图片预览同步上传功能
时间:2021-01-01 13:00:07|栏目:JavaScript代码|点击: 次
废话不多说了,直接给大家贴代码了,具体代码如下所示:
/** * Created by liujing on 2017/5/10. */ $(document).ready(function($) { function changef(which,bulk,name_n){ var bulka = bulk; var thisid = which.attr("id"); var f = which.prop ('files')[0]; var filename = f.name; var mime = filename.toLowerCase().substr(filename.lastIndexOf(".")); var liid = $("#"+thisid).closest("li").attr("id"); var liidb = parseInt(liid)+1; if(mime==".jpg" || mime==".png"){ var src = window.URL.createObjectURL(f); var name = $("#logo"+liid).val().split("\\").pop(); $('#logotips'+liid).css({"background":'url('+src+')','backgroundSize':100+'%'}); $('#up_flower'+liid).empty().append(name); $('#'+liid).append('<a class="edit_text">✖</a>'); bulka.append('<li class="uploading" id="'+liidb+'"><a class="a_a" id="logotips'+liidb+'"><input class="input_hide logo'+liidb+'" type="file" name="'+name_n+'" id="logo'+liidb+'"></a></li>'); $('#logotips'+liidb).css({"background":'url("../../img/c_up.png")','backgroundSize':100+'%'}); } $('.logo'+liid).addClass("newc"+liid); $('.newc'+liid).removeClass("logo"+liid); $('.newc'+liid).on('change',function(){ var files = $(this).prop('files')[0]; var srcb = window.URL.createObjectURL(files); var liid = $("#"+thisid).closest("li").attr("id"); var liidb = parseInt(liid)+1; var name = $("#logo"+liid).val().split("\\").pop(); $('#logotips'+liid).css({"background":'url('+srcb+')','backgroundSize':100+'%'}); $('#up_flower'+liid).empty().append(name); }) $('.edit_text').on('click',function(){ $(this).parent().remove(); }) } function changelast(which ,bulk){ var bulka = bulk; var thisid = which.attr("id"); var f = which.prop ('files')[0]; var filename = f.name; var mime = filename.toLowerCase().substr(filename.lastIndexOf(".")); var liid = $("#"+thisid).closest("li").attr("id"); var liidb = parseInt(liid)+1; if(mime==".jpg" || mime==".png"){ var src = window.URL.createObjectURL(f); var name = $("#logo"+liid).val().split("\\").pop(); $('#logotips'+liid).css({"background":'url('+src+')','backgroundSize':100+'%'}); $('#up_flower'+liid).empty().append(name); $('#'+liid).append('<a class="edit_text">✖</a>'); } $('.logo'+liid).addClass("newc"+liid); $('.newc'+liid).removeClass("logo"+liid); $('.newc'+liid).on('change',function(){ var files = $(this).prop('files')[0]; var srcb = window.URL.createObjectURL(files); var liid = $("#"+thisid).closest("li").attr("id"); var liidb = parseInt(liid)+1; var name = $("#logo"+liid).val().split("\\").pop(); $('#logotips'+liid).css({"background":'url('+srcb+')','backgroundSize':100+'%'}); $('#up_flower'+liid).empty().append(name); }) $('.edit_text').on('click',function(){ $(this).parent().remove(); }) } $(".logo1").one("change",function(){ that = $(this); var bulk = $("#add_beo"); var name = $(this).attr('name'); changef(that,bulk,name); }); $("#add_beo").one("change",".logo2",function(){ that = $(this); var bulk = $("#add_beo"); var name = $(this).attr('name'); changef(that,bulk,name); }); $("#add_beo").one("change",".logo3",function(){ that = $(this); var bulk = $("#add_beo"); var name = $(this).attr('name'); changef(that,bulk,name); }); $("#add_beo").one("change",".logo4",function(){ that = $(this); var bulk = $("#add_beo"); var name = $(this).attr('name'); changef(that,bulk,name); }); $("#add_beo").one("change",".logo5",function(){ that = $(this); var bulk = $("#add_beo"); changelast(that , bulk); }); });