我想捕获 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
的委托(delegate)不起作用。
我们如何使用委托(delegate)检测页面中的任何 iframe 加载?
最佳答案
How can we detect any iframe load from the page using delegation?
不幸的是,无法通过 jQuery 委托(delegate)捕获 iframe 加载事件。
关于javascript - 使用 jQuery 委托(delegate)从 iframe 捕获加载事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51645711/