jquery函数,用于获取预览图像的图像

jquery function for getting image of preview image

本文关键字:图像 获取 函数 用于 jquery      更新时间:2023-10-27

我有一个使用jquery创建图像预览的代码。。嗯,这很好。。但当我想要预览图像的高度和宽度时,问题就来了。。

这是我的jquery函数。。

$(function() {
    $('.cvrupload').change(function(){
        cvrpre(this);   
        var newfl_h = $('.cvr').find("img").height(), 
        newfl_w = $('.cvr').find("img").width();
        alert(newfl_h); // this line does not show any height, it shows null
        e.preventDefault();
    });
});
function cvrpre(input) {
    if (input.files && input.files[0]) {
          var reader = new FileReader();
          reader.onload = function (e) {
          $('.cvr').attr('src', e.target.result);
          }
       reader.readAsDataURL(input.files[0]);
     }
}

编辑

如果我使用$('.cvr').height()而不是$('.cvr').find("img").height(),那么它给出了$('.cvr') 中先前图像的高度

问题是,代码在cvrpre函数完成之前就继续了。您应该在cvrpre函数中返回width和height。所以它可以在上运行