Load事件不起作用(两个JS nore JQuery都不起作用)

问题描述 投票:1回答:1

我无法掌握加载事件。

我将正在处理的网站图像的默认不透明度设置为0。我希望它们在完全加载时显示。为此,我需要能够在加载时启动指令,该指令不起作用:

$('img').on('load', function() {
  $(this).css('opacity', '0')
})
img {
  opacity: 0
  -webkit-transition: all .3s;
  -o-transition: all .3s;
  transition: all .3s;
}
<img src="https://placeimg.com/640/480/any">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

我尝试过.load()、. one('load',function(){…}),也可以在带有'load'事件监听器的普通JS中使用。无效。

任何想法?

javascript jquery events load
1个回答
-1
投票

您是否尝试过:

document.addEventListener("DOMContentLoaded", function(event) {
    // your code here
});
window.onload = function() {
    // your code here
};

$(window).bind("load", function() {
    // your code here
});

否则,您能告诉我控制台输出什么吗?有任何错误吗?

© www.soinside.com 2019 - 2024. All rights reserved.