Detect when a particular element in an iframe is l

2019-07-26 16:35发布

问题:

Is there a way for me to detect when a particular element within an iframe is loaded without waiting for the full iframe content to load?

The problem I have is that the iframe is filled with many SVG buttons that take a long time to load and process and hooking into the load event of the iframe forces the action I want to take to wait for them to load fully. I want to access one element out of the iframe and wanted to know if it was possible to detect that that element has been loaded without waiting for the full contents of the iframe to load.

JavaScript or jQuery solutions are both file with me.

回答1:

$('iframe').onload = function() {
     alert("loaded")
})

This will fire when the HTML is loaded, but before it has been fully parsed and rendered.

Beyond that you'd have to poll to see of the element you're looking for is in the page.



回答2:

You can use it :

$("iframe").contents().find("your iframe elem").load(function(){
    alert('element loaded.');
});