使用 jQuery 委托从 iframe 捕获加载事件
Catch the load event from iframes, using jQuery delegation
我想捕获 load
甚至 any iframe,它在加载页面后的某个时间点附加到文档中。
我尝试使用 on("load", "iframe", fn)
,但似乎没有用:
function addIframe() {
$("<iframe src='https://example.com'>").appendTo($("body"))
}
// This does not work
$(document).on("load", "iframe", function() {
alert("loaded, caught via delegation")
})
setTimeout(function() {
addIframe()
// This works
$("iframe").on("load", function() {
alert("Loaded, caught via direct selection")
})
}, 1000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
虽然 $("iframe").on("load", fn)
,在附加 iframe 后立即起作用,但 document
上的委派不起作用。
我们如何使用委托从页面检测任何 iframe 加载?
How can we detect any iframe load from the page using delegation?
不幸的是,无法通过 jQuery 委托捕获 iframe 加载事件。
我想捕获 load
甚至 any iframe,它在加载页面后的某个时间点附加到文档中。
我尝试使用 on("load", "iframe", fn)
,但似乎没有用:
function addIframe() {
$("<iframe src='https://example.com'>").appendTo($("body"))
}
// This does not work
$(document).on("load", "iframe", function() {
alert("loaded, caught via delegation")
})
setTimeout(function() {
addIframe()
// This works
$("iframe").on("load", function() {
alert("Loaded, caught via direct selection")
})
}, 1000)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
虽然 $("iframe").on("load", fn)
,在附加 iframe 后立即起作用,但 document
上的委派不起作用。
我们如何使用委托从页面检测任何 iframe 加载?
How can we detect any iframe load from the page using delegation?
不幸的是,无法通过 jQuery 委托捕获 iframe 加载事件。