未确定的检查功能返回--- 马上它命中img.onload它会跳到checkize的末尾
var valor1 = 2;
var valor2 = 6;
var valor3 = 10;
function showResult() {
var result = parseInt(valor1 + valor2 + valor3);
console.log(result);
if (result >= 0 && result <= 20) {
newResult = result;
result = window.open();
result.document.write("<b> Su resultado es:</b> " + newResult +
" <br> <b> Muy Bajo </b> : Con esta puntuación debes saber que"
);
}
}
showResult();
答案 0 :(得分:0)
你缺少关闭主要功能的大括号,因为你的if (sizes.width != 600 || sizes.height != 800)
在img.load里面是错误的,把它改成看起来如下,你正在比较确切的长度所以只有精确的尺寸图像会返回true我已经更改了代码,以便在加载后将图像附加到正文并将条件if (sizes.width <= 600 || sizes.height <= 800) {
更改为800 x 600
,以使图像小于console
没有附加到正文,并且错误被抛出function checksize(files) {
var img = new Image();
img.onload = function() {
var sizes = {
width: img.width,
height: img.height
};
if (sizes.width <= 600 || sizes.height <= 800) {
console.log("Error: Image size should be greater 600 x 800px , image " + img.src + ' could not be loaded');
return false;
} else {
$('body').append(img);
return true;
}
};
img.src = files;
}
你可以改变条件
function checksize(files) {
var img = new Image();
img.onload = function() {
var sizes = {
width: img.width,
height: img.height
};
if (sizes.width <= 600 || sizes.height <= 800) {
console.log("Error: Image size should be greater 600 x 800px , image " + img.src + ' could not be loaded');
return false;
} else {
$('body').append(img);
return true;
}
};
img.src = files;
}
var files = ['https://www.animalfriends.co.uk/app/uploads/2017/04/21150218/iStock-524369488.jpg', 'https://static.pexels.com/photos/356378/pexels-photo-356378.jpeg', 'https://www.pets4homes.co.uk/images/articles/238/large/44186f3f7c3e962827e462a95d7206bc.jpg'];
for (var i = 0; i < files.length; i++) {
checksize(files[i]);
}
img {
width: 250px;
height: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
{{1}}
答案 1 :(得分:0)
您正在尝试将事件处理程序添加到图像元素的load事件中。这样做
var img = new Image();
$(img).on("load",function(){
var sizes = {
width: this.width,
height: this.height
};
if (sizes.width != 600 || sizes.height != 800)
{
alert("Error: Image size should be 600 x 800px");
return false;
}
else
{
return true;
}
});
img.src = _URL.createObjectURL(files[0]);
请注意,如果要在设置图像时触发该功能,则需要在事件函数之后放置img.src。将其放在函数内将导致递归