1

jqueryを使用してiframeを作成していますが、親と子の両方が同じドメインにあります(SOPエラーはありません)。

iframeを作成する前に、「data」要素を次のようにページノードにアタッチします

$('#anElement').data("test","This is a test");
console.log($('#anElement').data("test")); // This displays the correct "This is a test string"

この後、iframeを作成します

var iframe = $('<iframe>')
        // Set source
        .attr('src',src)
        // Set function to be called on load
        .load(function(){

            $('body').find('iframe')[0].contentWindow.test(); // This function is inside the iframe and it's called correctly 
            console.log($('#anElement').data()); // This displays undefined in console. 
        });
$('body').append(iframe); // Add it to the page

注意してください、私がする場合

console.log($('#anElement'));

'load'関数内で、コンソールは正しい要素を表示します。

私が問題を見つけるのを妨げる非常に些細なことがあるのではないかと心配していますが、誰かが私を助ける方法はありますか?

4

0 に答える 0