前一篇讲了窗口间的关系,下面来谈谈怎么交互。

说到底很简单,找到了所需要交互的窗口,就像访问本窗口内的对象来访问目标窗口内的变量、函数、 或html对象等。

当然也要举个例子。

a.htm:

<!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>
    <title>无标题页</title>
</head>
<body bgcolor="blue">
   <iframe src="b.htm" name="bChild"></iframe>
   
    <div ;
  }
 </script>b.htm

<!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>
    <title></title>
</head>
<body bgcolor="green">
    BBBBBBBB

    <input type="button" value="click" onclick="javascript:Show();" />
</body>
</html>
 <script type="text/javascript">
    function Show()
    {
       alert("父窗口的变量a的值= "+  window.parent.a);       
       alert("父窗口的函数GetString() =  "+  window.parent.GetString());  
       alert("父窗口的元素div的innerHTML =  "+  window.parent.document.getElementById("div1").innerHTML);     
    }
 </script>通过浏览器访问a.htm,点击click可得到相应的结果。如此一台,窗口间的操作就容易很多。

再举个窗口间传值的简单例子。

要求实现如下:在父窗口中打开一个子窗口,输入相关内容,关闭子窗口,将输入值返回到父窗口。

这是最常见的需求。

可以用window.open或window.showModalDialog来实现。

注意:showModalDialog仅适用于ie。

以下是例子。

a.htm

<!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>
    <title>无标题页</title>
</head>
<body bgcolor="blue">
   
    <input type="text" ).value = ary[1];
  }
 </script>b.htm

<!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>
    <title></title>
</head>
<body bgcolor="green">
      
    <input type="text" )
       {
            window.opener.SetValue(ary);
           
       }
       else
       {
            window.returnValue  =     ary;
       }
       window.close();
    }
 </script>运行a.htm可看到相关结果。

懂得了基本原理,不论情况有多么复杂,问题都会迎刃而解


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cpp2017/archive/2007/02/27/1515474.aspx

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-09-28
猜你喜欢
  • 2021-12-19
  • 2021-07-06
  • 2021-07-27
  • 2021-11-25
  • 2022-01-03
  • 2021-11-21
  • 2021-11-19
相关资源
相似解决方案