动态iframe onload无法启动? [英] Dynamic iframe onload not firing?

查看:103
本文介绍了动态iframe onload无法启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是可能的,因为有很多类似的问题(已经解决),但是似乎没有一个问题可以处理动态创建的iframe.

I figure this is possible because there are a ton of similar questions (that have been solved) but none seem to deal with dynamically created iframes.

基本上,我正在使用jquery创建一个临时iframe来加载文件(从通过ajax调用提供的文件路径中),然后打开文件下载提示.

Basically I'm using jquery to create a temp iframe to load files (from a file path served through an ajax call) to then open a file download prompt.

一切正常,但为了防止一堆iframe填充我的DOM,我希望在文件下载后将iframe删除.

Everything works but to keep my DOM from being populated by a bunch of iframes I would like the iframes to be removed once the file is downloaded.

我的iframe创建代码:

My iframe creation code:

$('<iframe class="downloadIFrame" src="'+data['url']+'"></iframe>').appendTo('body');

以及销毁它的代码(在就绪" jquery事件回调中):

And the code to destroy it (in the "ready" jquery event callback):

$('.downloadIFrame').live('load', function() { alert('loaded');$(this).remove(); });

我没有收到有关iframe已加载的警报,因此未调用它.有任何线索吗?

I don't get the alert that the iframe was loaded so it's not called. Any clue?

推荐答案

live适用于事件冒泡机制,其中iframe load事件未触发用户操作.因此,您不能使用live来处理iframe加载事件.

live works on event bubbling mechanism where as iframe load event is not user action triggered. So you cannot use live to handle the iframe load event.

此外,在设置iframe的src之前,还应附加iframe load事件处理程序.这是为了避免在附加load事件之前先加载iframe的情况.

Also before setting the src of the iframe you should attach the iframe load event handler. This is to avoid the case when iframe gets loaded before the load event is attached.

尝试一下

$('<iframe class="downloadIFrame"></iframe>')
.appendTo('body')
.load(function(){
   $(this).remove();
}).attr("src", data['url']);

这篇关于动态iframe onload无法启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆