iFrame中新页面加载时的jQuery事件 [英] jQuery Event when New Page Loads in iFrame
问题描述
我正在尝试在一个iframe加载新页面(可能通过用户点击链接)时触发的父项上设置一个事件。
I'm trying to set up an event on the parent that gets triggered when an iframe loads a new page (likely via the user clicking a link).
HTML:
<iframe name="posts" id="posts" src="edit.php">
JS:
$(document).ready(function() {
$('iframe#posts').live('load', function() {
console.log('live');
});
});
问题是 console.log()
语句从不被触发。我如何实现我想要做的事情?
The problem is that the console.log()
statement never gets triggered. How can I achieve what I'm trying to do?
推荐答案
我最好的猜测是iframe上的加载事件在文档上的ready事件之前已经触发,这意味着处理程序安装得太迟。您可能必须在 iframe
元素本身中使用旧版 onload
。
My best guess is that the 'load' event on the iframe has fired before the 'ready' event on the document, which means that the handler is installed too late. You may have to use the old-fashioned onload
in the iframe
element itself.
或者您可以将顶级脚本中的 $('iframe#posts')。live()
代码段放在处理程序。
Or you could just put the $('iframe#posts').live()
snippet in a top-level script rather than in the ready handler.
这篇关于iFrame中新页面加载时的jQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!