在img标签中上传图片

Viv*_*gon 6 html javascript css file-upload image

祝大家圣诞快乐.

我有4个部分,每个部分都包含一个空的img标签.在上传图像时,它必须适合相应的部分.

当我单击"在第1部分中添加图像"并上传图像时,它必须像Image_1 div一样修复所有四个.但是当我在我的代码中插入click功能时它无法工作.

这是我的错误.

<div class="pre_img" >
  <span>
     <img class="prw_img"  src="http://www.iconshock.com/img_jpg/REALVISTA/general/jpg/128/preview_icon.jpg" alt="your image" />
  </span> 
</div>

<input id="file" type="file" name="files[]" onChange="readURL(this);" />

<div id="Image_1">
    <button> AddImage to section 1</button>
    <img id="img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>

<div id="Image_2">
    <button> AddImage to section 2</button>
    <img id="img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>

<div id="Image_3">
    <button> AddImage to section 3</button>
    <img id="img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>

<div id="Image_4">
    <button> AddImage to section 4</button>
    <img id="img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc"/>
</div>
Run Code Online (Sandbox Code Playgroud)

继承我的剧本

function readURL(input) {
     if (input.files && input.files[0]) {
           var reader = new FileReader();

           reader.onload = function (e) {
                $('.prw_img,#img_1').attr('src', e.target.result).width(112).height(112);
                $('#img_1').css('display','inline');
           };
           reader.readAsDataURL(input.files[0]);
     }
}
Run Code Online (Sandbox Code Playgroud)

我已经制作了一个JSBIN以便于理解我没有添加点击并尝试添加该功能然后整个脚本无法正常工作

ext*_*ter 1

为图像部分指定一个类,或将它们包含在容器中以使事件侦听器更易于处理

HTML:

  <div class="pre_img">
    <span><img class="prw_img" src=
    "http://www.iconshock.com/img_jpg/REALVISTA/general/jpg/128/preview_icon.jpg" alt=
    "your image"></span>
  </div>

  <form>
    <input id="file" type="file" name="files[]" onchange="readURL(this);">
  </form>

  <div id="Image_1" class="imageSection"><button>AddImage to section 1</button> <img id=
  "img_1" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
  "img_1"></div>

  <div id="Image_2" class="imageSection"><button>AddImage to section 2</button> <img id=
  "img_2" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
  "img_2"></div>

  <div id="Image_3" class="imageSection"><button>AddImage to section 3</button> <img id=
  "img_3" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
  "img_3"></div>

  <div id="Image_4" class="imageSection"><button>AddImage to section 4</button> <img id=
  "img_4" alt="" width="100px" height="100px" style="Border 1px solid #ccc" name=
  "img_4"></div>
Run Code Online (Sandbox Code Playgroud)

然后使用以下脚本将上传的图像显示到类[在本例中为类activeImage],并将侦听器绑定到切换“活动”容器的按钮

杰斯:

$(".imageSection button").click(function() {
    $(".imageSection img").removeClass("activeImage");
    $(this).parent().find("img").addClass("activeImage");
});
$(".imageSection:eq(0) img").addClass("activeImage");

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function(e) {
            $('.prw_img,.activeImage').attr('src', e.target.result).width(112).height(112);

            $('.activeImage').css('display', 'inline');
        };

        reader.readAsDataURL(input.files[0]);
    }
}
Run Code Online (Sandbox Code Playgroud)

JsBin: http: //jsbin.com/imonub/8/edit