访问 .load() 函数中的 $(this) 元素

Access $(this) element inside .load() function

本文关键字:this 元素 函数 load 访问      更新时间:2023-09-26

我正在尝试在加载图像后从图像中删除loader icon。为此,我需要访问$(this)但我无法访问图像元素并删除js-image-loading类。

变量html包含通过 AJAX 获取的动态HTML内容。

    $(html).find('img').load(function() {
        $(this).removeClass("js-image-loading"); //ok image is done loading, remove icon
    });

这可能做到吗?

你忘了引用应该$('html')的html选择器$(html)但我建议你使用*

$('*').find('img').load(function() {
   $(this).removeClass("js-image-loading");
});

或者,干脆$('img').load()

试试

$("html").find('img').load(function () {
    $(this).removeClass("js-image-loading"); //ok image is done loading, remove icon
});

$('img').load(function () {
    $(this).removeClass("js-image-loading"); //ok image is done loading, remove icon
});

注意:html 是字符串而不是对象

演示