Pass jquery variables between iframe and parent

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

How can I work with jQuery and iframe. Get and pass var from iframe to body and from body to iframe. I have the following example. How can I click the button in iframe and make it take effect on the the body #test. How about ready var x in iframe.

 var x = "whatever"; 

Inside page.html i have

  

回答1:

$("#myid", top.document);  

or

$("#myid", parent.document.body);  

This will give you access to the container of the IFRAME

as per : http://groups.google.com/group/jquery-en/browse_thread/thread/5997ef4a60a123af



回答2:

From the parents perspective:

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalFunction(); 

OR

var iFrameValue = $('#iframe').get(0).contentWindow.myLocalVariable; 

From the iframe perspective

 


回答3:

You can use contentWindow and contentDocument properties, if same origin poilicy is not a problem. I've setup a demo. View code for the demo and code for iframe.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!