在javascript中查找图像的尺寸

gee*_*eth -1 javascript

我需要在上传之前检查图像的尺寸.我在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我知道这是一个重复的问题,但经过长时间的研究后我找不到解决方案.试过这里列出的解决方案如何使用JavaScript获取图像大小(高度和宽度)?,但没用.

Pra*_*mar 5

您可以使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)