图像预览会打开重复的div [英] Image preview opens for duplicated div

查看:86
本文介绍了图像预览会打开重复的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以上传图像并为这个图像添加一个预览,并且我有两个按钮,当我点击第一个按钮时,它会点击输入类型文件并打开它,第二个按钮会复制div ,我如何复制这个div并改变它的id或类?并在复制后,我怎么才能使按钮工作的新复制的div不是原来的唯一?这里是我的代码:



$(function(){$(input :file())。change(function(){if(this.files&& this.files [0]){var reader = new FileReader(); reader.onload = imageIsLoaded; reader.readAsDataURL(this.files [ 0));}});});函数imageIsLoaded(e){$('#ImgUpload')。attr('src',e.target.result);}; $('。uploadimg')。click function(){$('input:file')。click();});

  input {display:none;}  

< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js>< / script>< div class =uploader >< button type =buttonclass =uploadimg> Open uploader< / button>< input type =file>< img id =ImgUpload alt =您的图片/>< br>< br>< / div>< button type =button>重复按钮< / button>

无法正确呈现 jpg以exif方向旋转



第二件事是最好使用 URL.createObjectURL ,而不是使用base64 dataURL。这也引发了另一个问题,那就是大多数开发人员忘记撤销 objectURL的



这就是为什么我创建 Screw-FileReader ,因此您可以轻松地调用 blob.image()并获取一个带有自动撤销的对象url的图像,并且还有一个使用canvas创建的exif自动旋转。


$ b

var $ file = $( ')。(#')'。on('change',preview); $('。uploader button')。click(function(){$ file.click()})function preview(evt){var preview = document.querySelector ('.preview')Array.from(this.files).forEach(function(file){file.image().th en(function(img){preview.appendChild(img)},function(){// err not an image ...})})}

  input {display:none;}  

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js> < / script>< script src =https://cdn.rawgit.com/jimmywarting/Screw-FileReader/master/index.js>< / script>< div class =uploader> < button type =button>打开上传程式< / button> < input id =filepickertype =filemultiple accept =image / *> < div class =preview>< / div>< / div>  

第三件事我与Soltani Neji的回答是,当你在第一个上传按钮之前点击dublicate按钮时,它会产生意想不到的事情。它不能很好地跟踪索引。



现在接受我的建议:改用多个属性只需一个按钮就可以简化。它还有助于添加 accept =image / *以仅过滤图像


I have a function that uploads and image and adds a preview to this image, and i have two buttons, when i click on the first one it clicks on the input type file and opens it, and the second one duplicates the div, how can i duplicate this div and change it's id's or classes? and after duplicating it how can i make the buttons work for the new duplicated div not the original one only? here is my code:

$(function() {
$("input:file").change(function() {
if (this.files && this.files[0]) {
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
}
});
});

function imageIsLoaded(e) {
	$('#ImgUpload').attr('src', e.target.result);
};

$('.uploadimg').click(function(){
    $('input:file').click();
});

input{
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="uploader">
<button type="button" class="uploadimg">Open uploader</button>
<input type="file">
<img id="ImgUpload" alt="Your Image"/>
<br>
<br>
</div>
<button type="button">Duplicate button</button>

解决方案

One common problem I see with this file previews are that most of them are unable to correctly render jpg's rotated with exif orientation

The second thing is that it's better to use URL.createObjectURL rather then using a base64 dataURL. That also raises an other issue and that is that most developers forget to revoked objectURL's

That is why i have created Screw-FileReader so you can easily call blob.image() and get a image back with a object url that is automatically revoked and also has a exif auto-rotation built in using canvas.

var $file = $('#filepicker').on('change', preview);

$('.uploader button').click(function() {
  $file.click()
})

function preview(evt) {
  var preview = document.querySelector('.preview')
  
  Array.from(this.files).forEach(function(file) {
    file.image().then(function(img) {
      preview.appendChild(img)
    }, function() {
      // err not an image...
    })
  })
}

input {
  display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/jimmywarting/Screw-FileReader/master/index.js"></script>

<div class="uploader">
  <button type="button">Open uploader</button>
  <input id="filepicker" type="file" multiple accept="image/*">
  <div class="preview"></div>
</div>

the third thing i notis with Soltani Neji answer was that when you click dublicate button before the first upload button it dose unexpected things. it don't keep track of the index so well.

Now to my suggestions: Use the multiple attribute instead and just have one button to make it simpler. it also helps to add the accept="image/*" to filter out only images

这篇关于图像预览会打开重复的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆