加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

js中opener与parent的区别详细剖析

发布时间:2023-09-20 15:45:26 所属栏目:教程 来源:
导读:  opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的opener,在B页面通过opener对象可以访问A页面。

  parent表示父窗口,比如一个A页面利用iframe或frame调用B页
  opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的opener,在B页面通过opener对象可以访问A页面。
 
  parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent。在JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:a.html中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过window.opener(省略写为opener)来引用a.html,包括a.html的document等对象,操作a.html的内容。
 
  假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会出现“对象为空或者不存在”的JS错误。
 
  示例:
 
  aa.html
 
  代码如下:
 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
  <html xmlns="http://www.w3.org/1999/xhtml">
 
  <head>
 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
  <title>无标题文档</title>
 
  </head>
 
  <body>
 
  <span id="name"></span>
 
  <input type="button" " value="弹窗" onclick="window.open('bb.html')" />
 
  </body>
 
  </html
 
  bb.html
 
  代码如下:
 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
   <html xmlns="http://www.w3.org/1999/xhtml">
 
   <head>
 
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
   <title>无标题文档</title>
 
   </head>
 
   <body>
 
   <input type="text"  id="inputValue"/>
 
   <input type="button"  value="添加" onclick="window.opener.document.getElementById('name').innerHTML=inputValue.value"/>
 
   </body>
 
   </html>
 
  window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了aa.htm上的一个链接而打开了bb.htm,然后我们打算在bb.htm上输入一个值然后赋予aa.htm上的一个id为“name”的textbox中,就可以
 
  写为:
 
  window.opener.document.getElementById("name").value = "输入的数据";
 
  window.opener.document.getElementById("name").innerHTML= "输入的数据";
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章