在框架中,我用 JavaScript 获取 JSON 数据,组织成 HTML 代码,最后将其填充至上层文档的一个元素中。按照一般的写法,我们需要用到类似如下的语句:

1.window.parent.document.getElementById("myEle").innerHTML = html;

使用 jQuery ,写法如下:

1.$("#myEle", window.parent.document).html(html);

即指明了是在 window.parent.document 中查找 id=myEle 的元素。

随着前面的问题的解决(其实是对 jQuery 的了解不够),现在两种方案都可以实现我需要的效果了。

另外还有一种实现方式,代码如下:

1.parent.$("#myEle").html(html);

这种方法要求父文档也要调用 jQuery 。

今天总结一下js中几个对象的区别和用法:
首先来说说 parent.window与top.window的用法

"window.location.href"、"location.href"是本页面跳转

"parent.location.href"是上一层页面跳转
"top.location.href"是最外层的页面跳转
举例说明:

如果A,B,C,D都是jsp,D是C的iframe,C是B的iframe,B是A的iframe,如果D中js这样写

"window.location.href"、"location.href":D页面跳转

"parent.location.href":C页面跳转
"top.location.href":A页面跳转

现在终于明白了连接的时候target的用法了:
_blank:重新打开一个窗口
_parent:父窗口执行重定向

_self:自身页面重定向
_top:第一个父窗口重定向
综上所述可知:parent.window:父窗口对象
top.window:第一个父窗口的对象
下面来重点看看window.parent与window.openner区别
window.parent
是iframe页面调用父页面对象,当我们想从iframe内嵌的页面中访问外层页面是可以直接利用window.parent获取;
例子如下:

A.html

复制代码
代码如下:
<html>
<head>

<title>父页面</title>
</head>
<body>

<form id="form1" action="">
<div>
输入值:
<input
type="text" name="username" id="username" /><br />
<iframe
src="b.html" width="400px" height="300px"></iframe>
</div>

</form>
</body>
</html>

B.html

复制代码
代码如下:
<html>
<head>

<script type="text/javascript">
function getpValue()
{

document.getElementByIdx_x_x_x("span1").innerText=window.parent.document.getElementByIdx_x_x_x("username").value;

}
</script>
</head>
<body>

<span>文本框值为:</span><span id="span1"></span><br
/>
<input type="button" value="获取父窗口内的文本框值" onclick="getpValue();">

</body>
</html>

window.opener
是window.open或超链接<a> 打开的子页面调用父页面对象
例子如下
a.html

复制代码
代码如下:
<html>
<head>

<title>父页面</title>
<script type="text/javascript">

function openB()
{

window.open('b.html','b','width=400,height=200,status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=200,top=100');

}
</script>
</head>
<body>
<form
id="form1" action="">
<div>
输入值:
<input type="text"
name="username" id="username" /><br />
<input type="button"
value="打开窗口B" onclick="openB();" /><br />
<a href="b.html"
target="_blank">超链接打开B页面</a>
</div>
</form>

</body>
</html>

b.html

复制代码
代码如下:
<html>
<head>

<script type="text/javascript">
function getpValue()
{

document.getElementByIdx_x_x_x("span1").innerText=window.opener.document.getElementByIdx_x_x_x("username").value;

}
</script>
</head>
<body>

<span>文本框值为:</span><span id="span1"></span><br
/>
<input type="button" value="获取父窗口内的文本框值" onclick="getpValue();">

</body>
</html>

下面来举几个常用的例子:

parent.window与top.window一般在分割的页面即 frameset或iframe中使用

注销整个框架后返回到login.aspx:parent.window.location='Login.aspx'或者

top.window.location='Login.aspx'
window.parent也是常在框架中使用,

刷新:window.parent.location.reload();或者刷新某个框架:window.parent.MainForm.location.reload();

获得其他框架的元素值:window.parent.MainForm.form1.text1.value;

window.opener主要是获得通过超链接或者 window.open() 打开本身页面的页面的一些,比如获得值,刷新等

刷新:window.opener.location.reload();

获值:window.opener.document.getElement("txtName").value;

后退:top.playFrame.history.go(-1);
前进: top.playFrame.history.go(1);

刷新: top.playFrame.location.reload();
就总结到这里,这些对象很实用

js子窗口修改父窗口内容的更多相关文章

  1. 转-JS子窗口创建父窗口操作父窗口

    Javascript弹出子窗口  可以通过多种方式实现,下面介绍几种方法 (1) 通过window对象的open()方法,open()方法将会产生一个新的window窗口对象 其用法为: window ...

  2. JS打开新窗口,子窗口操作父窗口

    <!--父窗口弹窗代码开始--> <script type="text/javascript"> function OpenWindow() { windo ...

  3. Qt 代码: 子窗口调用父窗口(其实就是用指针直接访问)

    之前的 Qt 编程大多只涉及简单的多窗口,并未染指窗口间的传值交互,想来还是“涉世未深”,对 Qt 的理解.应用还需殷勤努力. 这次的问题是这样的,我想要实现一个类似QQ.阿里旺旺的聊天客户端,在弹出 ...

  4. 项目总结03:window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口

    window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口 项目中经常遇到一个业务逻辑:在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口(或局部更新A窗口)( ...

  5. JavaScript窗口打开与关闭及如何使用opener使子窗口对父窗口进行操作

    一.打开与关闭窗口 1.打开窗口:可以使用window对象中的Open()方法. newWindow = window.open(url,windowname,location); 参数说明: url ...

  6. frameset子窗口获取父窗口失败原因?

    报错信息: arrow.html:44 Uncaught SecurityError: Blocked a frame with origin "null" from access ...

  7. MFC子窗口和父窗口

    转载声明: 本文转载自:http://www.cnblogs.com/BeyondTechnology/archive/2011/03/25/1995934.html 感谢BeyondTechnolo ...

  8. MFC子窗口和父窗口(SetParent,SetOwner)

    一.概念和区别 在windows系统中,每个窗口对象都对应有一个数据结构,形成一个list链表.系统的窗口管理器通过这个list来获取窗口信息和管理每个窗口.这个数据结构中有四个数据用来构建list, ...

  9. jeecg项目子窗口获得父窗口元素id

    jeecg项目子窗口获得父窗口元素id, var parentWin = frameElement.api.opener;alert($(parentWin.document).find(" ...

随机推荐

  1. Oracle开启归档

    #查看数据库是否为归档模式select name ,log_mode from v$database; #改变归档模式到非归档模式shutdown normal/immediate; #关闭数据库st ...

  2. UI基础 - UITabBarController

    self.window = [[UIWindow alloc] init]; self.window.frame = [UIScreen mainScreen].bounds; oneViewCont ...

  3. JS和正则表达式验证手机号码、邮箱等

    <-------------------------------------验证手机号码---------------------------------------------> fun ...

  4. 转:ElasticSearch的安装和相关插件的安装

    原文来自于:http://blog.csdn.net/whxaing2011/article/details/18237733 本文主要介绍如下内容:          1.ElasticSearch ...

  5. .net反射详解

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...

  6. 转载:关于 Token,你应该知道的十件事

    关于 Token,你应该知道的十件事 原文地址:http://alvinzhu.me/blog/2014/08/26/10-things-you-should-know-about-tokens/ 原 ...

  7. 测试Flask+PYTHON的WEB框架

    参数URL: http://blog.csdn.net/qwiwuqo/article/details/8970621 安装flask之前,你必须要先安装python和easy_install. 安装 ...

  8. h.264全搜索以及快速全搜索算法

    Full Search 全搜索算法是最简单暴力的一种搜索算法,对搜索范围内的所有像素点都进行匹配对比,选出最合适的运动向量,以下就是一个搜索范围为4的全搜索范围(单个像素点) /*! ******** ...

  9. pl/sql 在一个程序块里打印日志输出到表格

    declare v_number NUMBER; v_number2 NUMBER; begin execute immediate 'truncate table t2'; insert into ...

  10. GCC使用

    GCC的选项 如何指定GCC的默认头 文件路径 Linux系统的头文件 和库文件搜索路径 头文件 库文件 运行时动态库的搜索路径 GCC的选项 -c 只生成目标文件(.o),不连接. % gcc -c ...