Maison > interface Web > js tutoriel > le corps du texte

父元素与子iframe相互获取变量和元素对象的具体实现_javascript技巧

WBOY
Libérer: 2016-05-16 17:19:52
original
1528 Les gens l'ont consulté

父中:

复制代码 代码如下:



var count = 1;


-------------------------------------------------------------
子iframe中:
复制代码 代码如下:


alert(window.parent.count); //获取父中的js变量
alert($("#username", window.parent.document));//获取父中jQuery对象
alert(window.parent.document.getElementByIdx_x('username'));//获取父中DOM对象


--------------------------------------------------------------------------------------------------

子iframe中(id="iframeId"name="iframeName"):
复制代码 代码如下:



varcount = 1;


-------------------------------------------------------------
父中:
复制代码 代码如下:


alert(document.iframeName.count);//获取子iframe中的js变量
alert($(window.frames["iframeName"].document).contents().find("#username"));//获取子iframe中jQuery对象
alert($("#username",document.frames("iframeName").document));//获取子iframe中jQuery对象
alert(window.document.getElementById('iframeId').contentWindow.document.getElementByIdx_x('username'));//获取子iframe中DOM对象


收集网上的一些示例:
用jQuery在IFRAME里取得父窗口的某个元素的值
只好用DOM方法与jquery方法结合的方式实现了

1. 在父窗口中操作 选中IFRAME中的所有单选钮
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");
2. 在IFRAME中操作 选中父窗口中的所有单选钮
$(window.parent.document).find("input:radio").attr("checked","true");
父窗口想获得IFrame中的Iframe,就再加一个frames子级就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");
3.在子窗口中调用父窗口中的另一个子窗口的方法(FRAME):
parent.frames["Main"].Fun();
注意:建议使用[],这样比较兼容多个浏览器,()火狐/搜狗/谷歌不兼容。
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!