模式窗体中调用父页面js与非模式化调用非父页面的js方法

最近项目中使用模式窗体,遇到以下问题记录一下:

模式窗体:你必须关闭该窗体,才能操作其它窗体;比如说,必须按确定或取消,或者按关闭。

非模式窗体:不必关闭该窗体,就可转换到其它窗体上进行操作。




一:非模式化窗体就是用
1.var val = parent.document.getElementById("txt1");   
var val = parent.document.getElementById("txt1");

  

二:模式窗体

  1. parent.html 
    <html>
    <head>
    <script language="text/javascript">
    function openwindow(){
       //这个父页面通过 retval获取子页面的window.returnValue返回来的值
        retval=window.showModalDialog("child.html")
        Alert(retval);
        document.getElementById('text1').value=retval
    }
    
    function Alert(msg){
        alert(msg);
    }
    </script>
    </head>
    <body>
    <form name=frm>
    <input name=text1 type=text id="text1"/>
    <input type=button onclick="javascript:openwindow()" value="Open window..">
    </form> 
    </body>
    </html>
    

      2. child.html

    <html>
    <head>
    <script language="javascript">
    function changeparent(){
      //通过window.returnvalue把值返回父页面
        window.returnValue="Value changed.."
        window.close()
    }
    </script>
    </head>
    <body>
    <form>
    <input type=button onclick="javascript:changeparent()" value="Change main window’s textbox value..">
    </form>
    </body>
    </html>
原文地址:https://www.cnblogs.com/panmy/p/5238784.html