jQuery 上下文未按预期工作

jQuery context not working as expected

在 Chrome devtools 中使用 $(CommandLineAPI querySelector) 执行此操作按预期工作:

$('body', iframe.contentDocument) // <body></body>

它获取 iframe 的主体。

对 jQuery 做同样的事情不会:

jQuery('body', iframe.contentDocument) // Empty jQuery result

有什么方法可以在 devTools 之外实现相同的功能?

确保在 iframe 准备就绪后访问它。

jQuery(iframe).load(function() {
    //Iframe is now loaded, so jQuery should be able to access the DOM:
    jQuery('body', iframe.contentDocument)
});