2016-11-01 27 views
0

我的图像预览有点问题,因为我想从#ImagePreview中将照片上传到#photo2上,点击确认按钮。点击获取图像预览到另一个div

https://jsfiddle.net/0xd4odyf/3/

HTML

<img id="ImagePreview"> 
<input type="file" class="InputFile" onchange="document.getElementById('ImagePreview').src =window.URL.createObjectURL(this.files[0])"> 
<div id="photo2"></div> 
<button id="confirm">confirm</button> 

回答

2

你可以得到你所选择的图像(#ImagePreview)的src属性,然后将其设置为DIV(#照片2)的background-image属性。

$("#confirm").click(function() 
{ 
    var img = $('#ImagePreview').attr('src'); 
    $("#photo2").css('background-image','url(' + img + ')'); 
}); 

实施例:https://jsfiddle.net/0xd4odyf/5/