使用jQuery load()事件来淡化图像(而不是纯JavaScript)

时间:2014-05-21 17:38:55

标签: javascript jquery events this target

我在JavaScript中编写了这段代码,以便在加载时使我网站的图像渐渐消失:

function loaded(e){
    var target = e.target;
    target.style.opacity = "1";
}

我通过HTML属性onload调用该函数,它运行正常....

现在,我尝试做同样的事情,但现在使用load() jQuery事件,只是为了避免从HTML属性调用该函数。所以我写道:

$('img').load(function(event){
    $(this).css('opacity', "1");
});

但它根本不起作用。

为什么?

提前感谢您的回答

1 个答案:

答案 0 :(得分:0)

您可以这样做:

img {opacity: 0.25;}

页面加载后:

$(document).ready(function(){
  $("img").css("opacity", 1);
});
相关问题