Access jQuery data from iframe element

前端 未结 2 1506
时光说笑
时光说笑 2020-12-13 16:25

Lets say I have a page on the same domain that I put inside an iframe. In the iframe, I have added data to an element, something like this:

HTML

2条回答
  •  庸人自扰
    2020-12-13 16:54

    You need to get the cache element from that window's jQuery object, like this:

    var windowjQuery = $('#frame')[0].contentWindow.$;
    var f = $('#frame').contents().find('#data');
    

    Then to get data, use $.data(), like this:

    windowjQuery.data(f[0], 'test1')
    

    You can test out your updated/working demo here.


    What this is really accessing is:

    var key = f[0][frame.contentWindow.$.expando];
    var dataItem = frame.contentWindow.$.cache[key]["dataKey"];
    

提交回复
热议问题