如何用js获取子级iframe页面里的变量

JavaScript016

如何用js获取子级iframe页面里的变量,第1张

方法只有一种:

步骤:

1、获取iframe节点

2、获取iframe中的变量

示例

1

2

3

4

5

6

7

8

9

a.html

<iframe

id='x'

name='x'

src='b.html'/>

b.html

<script>

var

y='1'

</script>

在a.html中获取y,则可以通过以下方式

document.frames['x'].y

//先获取frame,在获取y

先把array[i] =document.getElementById(ifr).contentWindow.document.getElementById(i)里的ifr加上引号,使之成为字符串,再调试下,看报什么错。你上面写的是ife,不是ifr。

1、在父页面访问Iframe子窗体的txtAddress控件

window.frames["ifrMapCompanyDetails"].document.all("txtAddress").value = '地址'

2、在Iframe子窗体1访问父页面的TextBox1控件 , 子窗体1把值赋给子窗体2的某个控件

string strValue = "从子窗体传递给父页面的值"

下面是在Page_Load事件里面调用的,当然可以写在javascript脚本里面

this.Response.Write("<script>parent.document.all('TextBox1').value = '" + strValue + "'</script>")

this.Response.Write("<script>if( parent.document.all('TextBox2').value = '0')parent.document.all('TextBox1').value = '44'</script>")

3、子窗体访问父窗体中的全局变量:

parent.xxx

4、在Iframe子窗体1访问子窗体2的txtAddress控件 子窗体1把值赋给子窗体2的某个控件

window.parent.frames["ifrMapCompanyDetails"].document.all("txtAddress").value = '地址'

父窗体提交两个Iframe子窗体

window.frames["ifrMapCompanyDetails"].Form1.submit()

window.frames["ifrMapProductInfoDetails"].Form1.submit()

Iframe子窗体 调用父页面的javascript事件

window.parent.XXX()

//父页面调用当前页面中IFRAME子页面中的脚本childEvent

function invokechildEvent()

{ var frm = document.frames["ifrChild1"].childEvent()}

或者调用当前页面中第一个IFRAME中的脚本childEvent

{ var frm = document.frames[0]frm.childEvent()}

//子页面调用父窗体的某个按钮的按钮事件

window.parent.Form1.btnParent.click()

父页面调用子窗体的某个按钮的按钮事件

window.frames['ifrChild1'].document.all.item("btnChild3").click()

//jquery 部分:

1.在父窗口中操作 选中IFRAME中的所有单选钮

$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true")

2.在IFRAME中操作 选中父窗口中的所有单选钮

$(window.parent.document).find("input[@type='radio']").attr("checked","true")