Run Javascript function when object content has been loaded

纵然是瞬间 提交于 2019-12-23 11:46:12

问题


How do I run a Javascript function when the content of an <object> has been loaded? The DOMContentLoaded event fires before that, and things that rely on it like JQuery's $() likewise.

Compare this to this. The first example fails because the function is executed when the external SVG hasn't been loaded. The second example polls for the elements it wants to change and only then executes the relevant code, and succeeds.

Polling works in practice, but is there a better solution for this? Ideally there would be an event fired that I can use.


回答1:


Does using the onload DomEvent work?

<object onload="changeColor()" data="circles.svg" type="image/svg+xml" id="circles"></object>

see my it here




回答2:


You should use onload/ready event of jquery - http://api.jquery.com/ready/

$('#circles').ready(function(){
    changeColor();
});`


来源:https://stackoverflow.com/questions/17347533/run-javascript-function-when-object-content-has-been-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!