js子窗口修改父窗口内容
在框架中,我用 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
代码如下:
<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
代码如下:
<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
代码如下:
<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
代码如下:
<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子窗口修改父窗口内容的更多相关文章
- 转-JS子窗口创建父窗口操作父窗口
Javascript弹出子窗口 可以通过多种方式实现,下面介绍几种方法 (1) 通过window对象的open()方法,open()方法将会产生一个新的window窗口对象 其用法为: window ...
- JS打开新窗口,子窗口操作父窗口
<!--父窗口弹窗代码开始--> <script type="text/javascript"> function OpenWindow() { windo ...
- Qt 代码: 子窗口调用父窗口(其实就是用指针直接访问)
之前的 Qt 编程大多只涉及简单的多窗口,并未染指窗口间的传值交互,想来还是“涉世未深”,对 Qt 的理解.应用还需殷勤努力. 这次的问题是这样的,我想要实现一个类似QQ.阿里旺旺的聊天客户端,在弹出 ...
- 项目总结03:window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口
window.open()方法用于子窗口数据回调至父窗口,即子窗口操作父窗口 项目中经常遇到一个业务逻辑:在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口(或局部更新A窗口)( ...
- JavaScript窗口打开与关闭及如何使用opener使子窗口对父窗口进行操作
一.打开与关闭窗口 1.打开窗口:可以使用window对象中的Open()方法. newWindow = window.open(url,windowname,location); 参数说明: url ...
- frameset子窗口获取父窗口失败原因?
报错信息: arrow.html:44 Uncaught SecurityError: Blocked a frame with origin "null" from access ...
- MFC子窗口和父窗口
转载声明: 本文转载自:http://www.cnblogs.com/BeyondTechnology/archive/2011/03/25/1995934.html 感谢BeyondTechnolo ...
- MFC子窗口和父窗口(SetParent,SetOwner)
一.概念和区别 在windows系统中,每个窗口对象都对应有一个数据结构,形成一个list链表.系统的窗口管理器通过这个list来获取窗口信息和管理每个窗口.这个数据结构中有四个数据用来构建list, ...
- jeecg项目子窗口获得父窗口元素id
jeecg项目子窗口获得父窗口元素id, var parentWin = frameElement.api.opener;alert($(parentWin.document).find(" ...
随机推荐
- 可重入与线程安全(Reentrancy and Thread-Safety)
http://blog.csdn.net/zzwdkxx/article/details/49338067 http://blog.csdn.net/zzwdkxx/article/details/4 ...
- 转:Java同步synchronized使用
原文链接 作者:Jakob Jenkov Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(s ...
- 【HDOJ】2571 命运
DP. /* 2571 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAX ...
- Linux&shell之结构化命令进阶
写在前面:案例.常用.归类.解释说明.(By Jim) for命令重复一系列的命令是一种常见的编程实践. #!/bin/bash # basic for command for test in A B ...
- URL图片预览(createObjectURL)
1.说明 1)createObjectURL 作用:创建url(creates a URL for the specified object); 语法:var url = URL.createObj ...
- 给大家介绍款在线压缩JS的工具
首先说下该工具的域名:http://javascriptcompressor.com/ 进入后界面如下: 具体要讲下它的功能点:在线压缩 Javascript 源码可以分不同的压缩级别:比如,一般情况 ...
- 实Schur分解
前面已经说过LU,Cholesky和QR分解,这次介绍的是实Schur分解.对这个分解的定义是任意一个矩阵A,可有如下形式的分解: U*A*U' = B;其中B是拟 ...
- POJ3080:Blue Jeans
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- Android中自定义Activity和Dialog的位置大小背景和透明度等
1.自定义Activity显示样式 先在res/values下建colors.xml文件,写入: view plainprint? 1. <?xml version="1.0" ...
- [AngularJS] ngMessageFormat
ngMessageFormat can be installed via npm using the following command: $ npm install angular-message- ...