ポップアップ ウィンドウの DOM で操作を実行しようとしていますが、何らかの理由ready
で、DOM に何かがある前に、ポップアップに対してイベントがすぐに発生します。
setTimeout
jQuery はコンテキストを使用してポップアップ ウィンドウの DOM にアクセスできること、および適切な時間が経過するまで任意のアクションを遅らせるために使用することで管理できることを知っています。
(function ($) {
$(function () {
var popup = window.open('/test');
// JSFiddle 404 page
$(popup.document).ready(function () {
// Should fire when the DOM of the 404 page has loaded...
$('h2', popup.document).css('color', '#FF0000');
// Change the color of the header to red.
console.log($('h2', popup.document).length);
// Should log 1
// Logs 0, though, because this function fires immediately, before the DOM loads.
});
setTimeout($.proxy(function () {
// This will definitely fire after the DOM of the 404 page is loaded.
var popup = this;
$('h2', popup.document).css('text-decoration', 'underline');
// This works, because it waited long enough.
// But I don't want to guess how long it will take the DOM to load....
}, popup), 5000);
// After 5 seconds...
});
})(jQuery);
また、jQuery のせいではないこともわかっています。console.log(popup.document.readyState);
の直後に追加するvar popup = window.open('/test');
と、 が出力されcomplete
ます。しかし、なぜですか?
何かアドバイス?
ありがとう!