-1

我需要在上传之前检查图像的尺寸。我在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

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

4

1 回答 1

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);
    }
}
于 2018-05-03T07:16:38.170 回答