html的input图片上传,前端通过input file原生简单实现上传图片预览功能

.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;

}

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

X

        btn.png

       

 

X

        btn.png

       

X

        btn.png

       

$(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='

'+

'

'+

''+

'X'+

                    'btn.png'+

                    ''+srcs+''+

'

'+

'

';

$(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();

}

})

})

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值