在iframe中如何共享 jQuery $.data?

发布于 2020-09-19 14:49:04

在父页面

<div id="main"></div>
<iframe src="子页面" />

$("#main").data('main_data',{'name':'izhangbo.cn'});

在iframe子页面中,如何获得 $("#main").data('main_data')的值?

查看更多

浏览量
2037
猫哥
猫哥 项目组成员 2020-09-19
希望我的回答能对你有所帮助
2 个回答
猫哥
猫哥 项目组成员 2020-09-19
希望我的回答能对你有所帮助

jQuery object itself is created inside anonymous function and it uses closure for accessing global (global for other jQuery functions) array inside this functions. So, as result: jQuery in iframe and jQuery in the top window have different data array. If you need top level data, use window.top.jQuery('#div1').data('test1') (please note that default context for jQuery is document, where it was initially created, so using "top level jQuery" we don't need to specify top level document.

学习
记录

发布
问题

分享
好友

手机
浏览

扫码手机浏览