【问题标题】:Find dimensions of image in javascript在javascript中查找图像的尺寸
【发布时间】:2018-10-13 09:36:30
【问题描述】:

上传前我需要检查图片的尺寸。我在 javascript 中创建了一个用于验证的函数,如下所示

$('#destSubmit').click(function() { 
    var img = document.getElementById('destImage'); 
    var width = img.naturalWidth ; 
    var height = img.naturalHeight ; 
    console.log('width : ' + width + '|| Height : ' + height);
    if(height != 100 || width != 100){
        alert(error);
    }
}

但它显示宽度和高度为undefined;

我知道这是一个重复的问题,但经过长时间的研究后我找不到解决方案。 尝试了此处列出的解决方案How to get image size (height & width) using JavaScript?,但没有用。

【问题讨论】:

标签: javascript


【解决方案1】:

您可以使用jQuery,如下所示

$('#destSubmit').click(function() {        
    var width = $("#destImage").width(); 
    var height = $("#destImage").height(); 
    console.log('width : ' + width + '|| Height : ' + height);
    if(height != 100 || width != 100){
        alert(error);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多