Jquery/javascript 检查 div 是否完成加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14192379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:57:57  来源:igfitidea点击:

Jquery/javascript check if the div finished loading?

javascriptjquery

提问by Max Pain

I have a facebook like button on my website like that:

我的网站上有一个类似 facebook 的按钮,如下所示:

<div class="fb-like" data-href="https://www.facebook.com/xxx"
             data-send="false" data-width="300" 
             data-show-faces="true">
</div>

I want to use jQuery or java script to check if this div is 100% finished loading on the page and then do other stuff.

我想使用 jQuery 或 java 脚本来检查这个 div 是否在页面上 100% 完成加载,然后做其他事情。

I used $(document).readyand .loadto check but non of them worked (they do the function even before the div finished loading). Does this problem have to do anything with the fact that this div contain an iframe from another website ? what am I doing wrong here ?

我使用$(document).ready.load检查但没有一个工作(他们甚至在 div 完成加载之前就完成了这个功能)。这个问题是否与这个 div 包含来自另一个网站的 iframe 的事实有关?我在这里做错了什么?

UPDATEI just tried this and it did not work at all:

更新我刚试过这个,它根本不起作用:

$(document).ready(function() {
    $(".fb-like").load(function (){
         alert("Loaded :)");
    });
});

回答by Undefined

I believe the issue is that you need to test if the iframe is loaded rather than the div. I would suggest changing your selector to include the child iframe and checking for the load event on this.

我认为问题在于您需要测试是否加载了 iframe 而不是 div。我建议更改您的选择器以包含子 iframe 并检查加载事件。

$('.fb-like iframe').load(function() {
  console.log('iframe loaded');
});

回答by Zeal Murapa

$('.fb-like iframe').on(function() {
  console.log('iframe loaded');
 });