js弹框怎么获得父页面的元素

JavaScript015

js弹框怎么获得父页面的元素,第1张

js获取父页面的元素可以用$(window.parent.document).find("#customer_id").val()这里的customer_id表示父页面某一个元素的id。

比如:父页面有一个隐藏的input框<input id="customer_id" type="hidden" value="${distributor.customer_id}"/>,那么在子页面就可以用上述的语句取到父页面的id为customer_id的值。

1、打开编辑工具editplus,点击editplus菜单栏上的【File】-->New -->HTML page。

2、新建好html页面后,editplus自动帮助我们生成了框架,先把title修改下,然后保存。

3、在body区域里添加了子,父,祖父三级div,代码如下。

4、演示代码写好后,打开浏览器,在浏览器上运行看看效果。

5、下面在head区域里添加实现js获取最高父级的代码,代码具体如下,这样利用了while循环来判断的。

6、实现好后,保存代码,此时再到浏览器上刷新访问,看下,此时弹出对话框告诉最后父级是zufu。

取父窗口的元素方法:

$(selector, window.parent.document)

那么你取父窗口的父窗口的元素就可以用:

$(selector, window.parent.parent.document)

类似的,取其它窗口的方法大同小异,

$(selector, window.top.document)

$(selector, window.opener.document)

$(selector, window.top.frames[0].document)

js:

父窗口:

1、<input type="text" name="currentProjectIDForDetail" id="currentProjectIDForDetail"

disabled

2、<input type="button"

onclick="window.open('showDetails.html','','toolsbar=no,menubar=no,resizable=yes,scrollb

ars=yes')" value="查看已有明细" id="showDetail" />

子窗口:

curproject = window.opener.document.getElementById("currentProjectIDForDetail").value

jQuery:

父窗口:

<input type="text" name="aa" id="aa" />

<input type="button"

onclick="window.open('son.html','','toolsbar=no,menubar=no,resizable=yes,scrollbars=yes')

" value="send" />

子窗口:

<script>

$(function () {

temp=$("#aa",window.opener.document).val()

$("#bb").html(temp)

})

</script>

</head>

<body>

<div id="bb"></div>