javascript - 如何在div中调整上传图片的预览?

标签 javascript jquery html css

需要在 div 中放置一个图像,而不是将图像作为 div 的背景图像

我的代码如下

<div style="display:none" id="previewImage" height="300px" width="300px">
    <img src="" id="previewImg" height="400px" width="450px">
</div>
<div>
    <input type='file' class="inputfile" name='file' id='file' size='50' accept='image/*' />
    <input type='submit' value='Upload' name='Upload image'/>
</div> 

最佳答案

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

    reader.onload = function(e) {
      $('#passport_photo')
        .attr('src', e.target.result)
        .width(150)
        .height('auto');
    };

    reader.readAsDataURL(input.files[0]);
  }
}
$("#imgpath").change(function() {
  readURL(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="imgpath" name="file" accept="image/*">

<div class=" ffset-2" >
  <img id="passport_photo" src="#" alt="image" style="width:150px;height:auto;">
</div>

关于javascript - 如何在div中调整上传图片的预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756627/

相关文章:

html - Z-Index 与 sibling 的 child

javascript - 从动态 html 导出 pdf

javascript - 无法获取未定义或空引用的属性 'indexOf'

javascript - 设置 SVG 路径中对象的位置

javascript - Fullcalendar - 拖动多个外部事件 - 结果代表日历上的 1 个事件

javascript - 颜色的 CSS 选择器,与表示无关

javascript - 使用 jquery 将字符串拆分为特定字符处的换行符

javascript - 将日期从 gergorian 转换为 shamsi

javascript - 爆炸或移动网格远离中心

javascript - 无法使用变量访问数组值