zoukankan      html  css  js  c++  java
  • 模式窗口window.showModal 岁月无情

    模式窗口的用法
    一、问题:

        我想弹出一个窗口,然后在弹出的窗口里,选择或输入一些信息,要求这些信息返回到父页面。
    建立父页面:a.htm

    <html>
    <head>
        <title></title>
        <mce:script language="javascript" type="text/javascript"><!--
            function OpenWin()
            {
                var getv = showModalDialog("b.htm", "", "dialogWidth:320px; dialogHeight:200px;status:no;help:yes");
                if (getv != null)
                {
                    document.forms[0].txtName.value=getv.split(",")[0];;
                    document.forms[0].txtAge.value=getv.split(",")[1];;
                }
            }
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <input type="text" name="txtName" id="txtName" />
        <input type="text" name="txtAge" id="txtAge" />
        <input type="button" name="Submit" value="打开" onclick="OpenWin()" />
        </form>
    </body>
    </html>
     

    建立子页面:b.htm

    <html>
    <head>
        <title></title>

        <mce:script language="javascript" type="text/javascript"><!--
            function GetValue()
            {
                //window.returnValue存放子窗口向父窗口所传值的内容
                window.returnValue=document.forms[0].txtName.value+","+document.forms[0].txtAge.value;
                window.close();
            }
       
    // --></mce:script>

    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <br />
        姓名:<input name="txtName" type="text" id="txtName" /><br />
        年龄:<input name="txtAge" type="text" id="txtAge" />
        <input type="button" name="Submit" value="关闭" onclick="GetValue()" />
        </form>
    </body>
    </html>
     

    这里利用了模式窗口window.showModalDialog(),利用window.returnValue = window.showModalDialog(sURL [, vArguments] [, sFeatures]),我们可以打开一个模态窗口,该窗口的优点是限制用户只能对当前的页面进行操

    作,而对其父页面不能进行操作,常用于向导或者信息获取页面。利用其中的vArguments我们可以在父页面和弹出的页面中进行参数的传递,参数可以为自定义的对象,也可以传递父页面中任何一个控件的引用,这使得我们可以很容

    易的来操作父页面中的各个元素,使得参数的传递变得非常容易。

    二、第2个参数的讨论
    showModalDialog("子页面地址","向子页面传递参数","子页面外观设置"),关于参数的详细设置看最后的注释,这里主要研究下第2个参数。
    1.第2个参数为自定义对象
    我们可以定义一个javascript对象,然后定义各种自定义属性的值,然后可以将此对象传递到子页面中。
    例:将父页面的信息封装成一个对象,然后将该对象传递给子页面。
    父页面:oneParent.htm

    <html>
    <head>
        <title>父页面向子页面传递自定义对象参数</title>
        <mce:script language="javascript" type="text/javascript"><!--
            function OpenWin()
            {
                //发送页面,利用模式窗口的第2个参数向子页面传递一个对象
                var person=new Object();
                person.myName=document.forms[0].txtName.value;
                person.myAge=document.forms[0].txtAge.value;
                window.showModalDialog("oneSon.htm",person,""); //第2个参数是一个对象
            }
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" method="post">
            姓名:<input type="text" name="txtName" id="txtName" />
            年龄:<input type="text" name="txtAge" id="txtAge" />
            <input type="button" name="Submit" value="打开" onclick="OpenWin()" />
        </form>
    </body>
    </html>
     
    子页面:oneSon.htm

    <html>
    <head>
        <title></title>

        <mce:script language="javascript" type="text/javascript"><!--
           window.onload=function(){
                //对话框通过window.dialogArguments来取得传递进来的参数。  
                var person=window.dialogArguments;
                document.forms[0].txtName.value=person.myName;
                document.forms[0].txtAge.value=person.myAge;
            };
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <br />
        姓名:<input name="txtName" type="text" id="txtName" /><br />
        年龄:<input name="txtAge" type="text" id="txtAge" />
        <input type="button" name="Submit" value="关闭" onclick="window.close()" />
        </form>
    </body>
    </html>
     

        以上代码中,文档全部加载完,才执行window.onload指定函数,以找到document.forms[0].txtName对象,如果去掉,window.onload=function(){}直接写大括号里的代码,则必须把这个JS代码块房到</body>之后才能正确运行,否则出现运行时间错误,找不到document.forms[0].txtName对象

    2.第2个参数是父页面的一个元素
    我们可以将父页面中元素对象的引用传递给子页面,通过该引用我们可以访问父页面中的该元素对象。
    例:其中利用元素对象的引用我们可以操纵父页面的元素对象的属性。
    父页面:twoParent.htm

    <html>
    <head>
        <title>父页面向子页面传递父页面元素</title>

        <mce:script language="javascript" type="text/javascript"><!--
            function OpenWin()
            {
                //发送页面,利用模式窗口的第2个参数向子页面传递一个对象
                window.showModalDialog("twoSon.htm",parentDiv,""); //第2个参数是父页面中的元素对象
            }
       
    // --></mce:script>

    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <div id="parentDiv">
            姓名:<input type="text" name="txtName" id="txtName" />
            年龄:<input type="text" name="txtAge" id="txtAge" />
            <input type="button"  value="打开" onclick="OpenWin()" />
        </div>
        </form>
    </body>
    </html>
     
    子页面:twoSon.htm

    <html>
    <head>
        <title></title>

        <mce:script language="javascript" type="text/javascript"><!--
    //       window.onload=function(){
                //对话框通过window.dialogArguments来取得传递进来的参数。  
                var infoKeYi=window.dialogArguments;
    //        };
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" >
        <div id="childDiv">
        </div>
        <br />
        <!-- 注释:显示传递进来的,父页面中的元素信息-->
        <input type="button" value="显示父页面传递的信息" onclick="childDiv.innerHTML=infoKeYi.innerHTML" />
        <input type="button" value="子页面操作父页面的信息", onclick='infoKeYi.style.backgroundColor="green"'/>
        <!-- 注释:显示传递进来的父元素信息-->
        <input type="button" name="Submit" value="关闭" onclick="window.close()" />
        </form>
    </body>
    </html>
     

          这里注释掉window.onload=function(){},直接写大括号里的代码,目的让JS代码先加载,后加载<body>里的元素,否则出现找不到对象infoKeYi的错误。

    3.第2个参数是window.
    如果第2个参数是window,这样可以取得父窗口的一些数据和方法。如:
    //取得父窗口的JS变量 var
    window.dialogArguments.父窗口中的变量;
    //获得父窗口的对象和属性
    window.dialogArguments.form1.父窗口中控件name.value ;
    //调用父窗口的方法 fun
    window.dialogArguments.父窗口中的方法 ;
    父页面:threeParent.htm

    <html>
    <head>
        <title>父页面向子页面传递父页面元素</title>
        <mce:script language="javascript" type="text/javascript"><!--
            //打开窗口
            function OpenWin()
            {
                window.showModalDialog("threeSon.htm",window,"dialogWidth:300px;dialogHeight:200px;status:no"); //第2个参数是window对象
            }
            //定义一个子窗口要调用的变量
            var age=20;
            //定义一个子窗口要调用的方法
            function getHello()
            {
                return "你好,世界!";
            }
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <div id="parentDiv" align="center">
            姓名:<input type="text" name="txtName" id="txtName" />
            <input type="button"  value="打开" onclick="OpenWin()" />
        </div>
        </form>
    </body>
    </html>
     

    子页面:threeSon.htm

    <html>
    <head>
        <title></title>

        <mce:script language="javascript" type="text/javascript"><!--
           window.onload=function(){
                //对话框通过window.dialogArguments来取得传递进来的参数。  
               
                //获得父窗口的对象和属性
                document.forms[0].txtName.value=window.dialogArguments.form1.txtName.value;
                //取得父窗口的JS变量
                document.forms[0].txtAge.value=window.dialogArguments.age;
                //调用父窗口的方法
                document.forms[0].txtSay.value=window.dialogArguments.getHello();
            };
       
    // --></mce:script>
    </head>
    <body>
        <form id="form1" runat="server" >
        <div id="childDiv" align="center">
        </div>
        <br />
        <!-- 注释:显示传递进来的,父页面中的元素信息-->
        姓名:<input name="txtName" type="text" id="txtName" /><br />
        年龄:<input name="txtAge" type="text" id="txtAge" /><br />
        问候:<input name="txtSay" type="text" id="txtSay" /><br />
        <input type="button" name="Submit" value="关闭" onclick="window.close()" />
        </form>
    </body>
    </html>
     


    三、注释:
    1、window.open()参数:
    例如:

    <SCRIPT LANGUAGE="javascript">
    <!--
     window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')
    //写成一行
    -->
    </SCRIPT> 


     参数解释:
    <SCRIPT LANGUAGE="javascript"> js脚本开始;
       window.open 弹出新窗口的命令;
       'page.html' 弹出窗口的文件名;
       'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替;
       height=100 窗口高度;
       width=400 窗口宽度;
       top=0 窗口距离屏幕上方的象素值;
       left=0 窗口距离屏幕左侧的象素值;
       toolbar=no 是否显示工具栏,yes为显示;
       menubar,scrollbars 表示菜单栏和滚动栏。
       resizable=no 是否允许改变窗口大小,yes为允许;
       location=no 是否显示地址栏,yes为允许;
       status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
    </SCRIPT>

    2、window.showModalDialog()参数:
    vReturnValue=window.showModalDialog(sURL    [,    vArguments]    [,sFeatures])  
    例如:

    <SCRIPT LANGUAGE="javascript">
    <!--
     window.showModalDialog('Ori_Photo.aspx','','resizable:yes;scroll:yes;status:no;dialogWidth=320px;dialogHeight=230px;center=yes;help=no');
    //写成一行
    -->
    </SCRIPT

    参数说明:  
       sURL--  
       必选参数,类型:字符串。用来指定对话框要显示的文档的URL。  
       vArguments--  
       可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。  
       sFeatures--  
       可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。  
       1.dialogHeight    :对话框高度,不小于100px,IE4中dialogHeight    和    dialogWidth    默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。  
       2.dialogWidth:    对话框宽度。  
       3.dialogLeft:    离屏幕左的距离。  
       4.dialogTop:    离屏幕上的距离。  
       5.center:    {yes|no|1|0}:窗口是否居中,默认yes,但仍可以指定高度和宽度。  
       6.help:    {yes|no|1|0}:是否显示帮助按钮,默认yes。  
       7.resizable:    {yes|no|1|0}    [IE5+]:是否可被改变大小。默认no。  
       8.status:    {yes|no|1|0}    [IE5+]:是否显示状态栏。默认为yes[    Modeless]或no[Modal]。  
       9.scroll:{    yes|no|1|0|on|off}:指明对话框是否显示滚动条。默认为yes。  
       下面几个属性是用在HTA中的,在一般的网页中一般不使用。  
       10.dialogHide:{    yes|no|1|0|on|off}:在打印或者打印预览时对话框是否隐藏。默认为no。  
       11.edge:{    sunken|raised}:指明对话框的边框样式。默认为raised。  
       12.unadorned:{    yes|no|1|0|on|off}:默认为no。 


    文章出处:飞诺网(www.diybl.com):file:///E:/架构设计学习/JavaScript积累/模式窗口window_showModal…-Html实例%20-%20diybl.mht

  • 相关阅读:
    protobuf使用遇到的坑
    嵌入式开发入门心得记录
    vim编辑模式下黑色背景,下来过程中出现白条的问题
    linux中awk的应用
    ntp时间同步
    mysql5.5适配
    centos 安装 epel
    ubuntu jdk安装
    add_header Access-Control-Allow-Origin $http_Origin always;
    111
  • 原文地址:https://www.cnblogs.com/huyinyang/p/2524336.html
Copyright © 2011-2022 走看看