Jquery 删除小图像

Jquery 删除小图像,jquery,Jquery,我有以下脚本来删除太小但无法正常工作的图像,第一次加载页面时,每个图像都会被替换为无图像.png,刷新页面后,图像正常工作,我缺少什么 $(document).ready(function () { $('.story-img').error(function () { $(this).attr("src", "/Images/no-image.png"); $(this).css('border', 'none'); }); $(".s

我有以下脚本来删除太小但无法正常工作的图像,第一次加载页面时,每个图像都会被替换为
无图像.png
,刷新页面后,图像正常工作,我缺少什么

$(document).ready(function () {
    $('.story-img').error(function () {
        $(this).attr("src", "/Images/no-image.png");
        $(this).css('border', 'none');
    });

    $(".story-img").each(function () {

        var theImage = new Image();
        theImage.src = $(this).attr("src") || $(this).src;

        var imageWidth = theImage.width;
        var imageHeight = theImage.height;

        if (imageWidth < 32 || imageHeight < 32 || $(this).height() < 32 || $(this).width < 32) {
            $(this).attr("src", "/Images/no-image.png");
            $(this).css('border', 'none');
        }

    });
});
$(文档).ready(函数(){
$('.story img')。错误(函数(){
$(this.attr(“src”,“/Images/no image.png”);
$(this.css('border','none');
});
$(“.story img”)。每个(函数(){
var theImage=新图像();
theImage.src=$(this.attr(“src”)| |$(this.src);
var imageWidth=图像宽度;
var imageHeight=图像高度;
如果(imageWidth<32 | | imageHeight<32 | |$(此).height()<32 | |$(此).width<32){
$(this.attr(“src”,“/Images/no image.png”);
$(this.css('border','none');
}
});
});

这可能是因为在页面html加载完成之前,图像数据尚未加载完成。您可以使用javascript加载事件,该事件将等待所有图像加载完成。使用

$(window).load(function(){}

您可能在加载图像之前检查图像的宽度和高度(使其宽度和高度等于0)。您可以使用
$(image).load()


您知道
imageWidth
imageHeight
有哪些值吗?我猜这是因为图像尚未加载,因此宽度和高度为0。您可以尝试用
.load
替换
.each
。顺便说一句:以
| |$(this).src结尾的行
应该是
| | this.src
@Thomas,谢谢你的提示,我会尝试进行更改,看看是否有帮助。如果更改后问题仍然存在,请制作一把小提琴让我们玩。或者你可以使用类似
的东西指定图像的宽度和高度,而不是将小提琴作为实际网站的链接。
$(".story-img").load(function () {

    //...

});