在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')的值?

查看更多

关注者
0
被浏览
37
bobneo
bobneo 认证专家 2020-09-19
为自由而编程,因编程而自由。联系QQ:632906391
2 个回答
bobneo
bobneo 认证专家 2020-09-19
为自由而编程,因编程而自由。联系QQ:632906391

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.

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览