jQuery onload - .load() - 事件不适用于动态加载的iframe [英] jQuery onload - .load() - event not working with a dynamically loaded iframe
问题描述
我的脚本在用户点击一个按钮时加载一个iframe。我想在iframe加载后调用另一个函数,但它不起作用。 iframe的确可以正常加载,但是在加载iframe之后永远不会调用.load()事件。 iframe包含来自同一网站的内容(无外部内容)。奇怪的是,我在我的代码中的其他几处使用了下面描述的相同技术,并且它完美地工作。任何人都可以提出我应该检查什么?
以下是相关的代码:
alert('iframe loaded');
}); $('#someButton')。live('click',function(){
$('body')。append($('< iframe id =myIframe src =https://www.mywebsite.comwidth =100%>< / iframe>'));
});
似乎有点类似于这里的问题:
这样,iframe元素就存在于带有append的Dom中。只有当您设置Url时,它才会加载,您将在哪一点附加您希望在结尾标记的加载函数。
所以对于你的代码
$ $ $ $'''someButton'。live('click',function(){
$ b $('body')。append($('< iframe id =myIframewidth =100%>< / iframe>'));
$ ('#myIframe')。attr('src',https://www.mywebsite.com);
$('#myIframe')。load(function(){
alert('iframe loaded');
});
});
My script loads an iframe when the user clicks a button. I want to call another function after the iframe loads, but it's not working. The iframe does load normally, but the .load() event never gets called after the iframe finishes loading. The iframe contains content from the same website (no external content). The strange thing is that I'm using the same technique described below at a few other points in my code and it works perfectly. Can anyone suggest what I should check next?
Here's the relevant code:
$('#myIframe').load(function () {
alert('iframe loaded');
});
$('#someButton').live('click', function () {
$('body').append($('<iframe id="myIframe" src="https://www.mywebsite.com" width="100%"></iframe>'));
});
Seems somewhat similiar to the question here:
jQuery .ready in a dynamically inserted iframe
This way the Iframe element exists in the Dom with the append. Only when you set the Url will it load, by which point you've attached the load function you wish to tag on the end. So for your code
$('#someButton').live('click', function () {
$('body').append($('<iframe id="myIframe" width="100%"></iframe>'));
$('#myIframe').attr('src',"https://www.mywebsite.com");
$('#myIframe').load(function () {
alert('iframe loaded');
});
});
这篇关于jQuery onload - .load() - 事件不适用于动态加载的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!