代码之家  ›  专栏  ›  技术社区  ›  Anand Shah

这个代码段的jquery代码是什么?

  •  0
  • Anand Shah  · 技术社区  · 15 年前

    我有一个父页面,其中包含一个文本区域和一个打开子窗口的链接。子窗口有另一个文本区域和一个按钮。用户在子窗口的文本区域中输入一些文本并单击按钮,就会触发一个javascript,它用子窗口的文本区域的内容更新父窗口中文本区域的内容,并关闭子窗口。

    我目前在javascript中做这个,它工作得很好,但是由于我们很快将转到jquery,那么如何使用jquery来完成相同的工作呢?

    page1.html
    ----------
    
    <script type="text/javascript">
        function newwin() {
            window.open('page2.html','','width=600');
        }
    </script>
    <body> 
        <textarea id='t1'>
        <a href="javascript:void(0)" onclick="newwin();">Click here</a> 
    
    </body>
    
    
    page2.html
    ----------
    <script type="text/javascript">
        function updateparent() {
            window.opener.document.getElementById('t1').value = document.getElementById('t2').value;
            window.close();
        }
    </script>
    <body> 
        <textarea id='t2'>
        <input type="submit" onclick="updateparent();"> 
    </body>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Mutation Person    15 年前

    有趣的问题。

    正如多梅尼克·罗杰所提到的,如果它起作用,那么你可能想离开它。

    对于我来说,问题不是“这个代码片段的jquery代码是什么?”但是如何使用jquery来实现相同的解决方案。

    有时,这不仅仅是简单的代码替换。采取以下措施:

    function updateparent() { 
        window.opener.document.getElementById('t1').value = document.getElementById('t2').value; 
        window.close(); 
    } 
    

    现在,jquery代码可能是这样的:

    function updateparent() { 
        window.opener.$("#t1").val($("#t2").val());
        window.close(); 
    } 
    

    但我不会这么做的。我将使用jquery用户界面中可用的弹出窗口功能,或者一些插件(例如 blockui )打开弹出窗口。

    此外,此代码:

    <body>  
        <textarea id='t2'> 
        <input type="submit" onclick="updateparent();">  
    </body>
    

    在jquery中,我们鼓励使用事件的后期绑定,因此任何内联javascript都将:

    <body>  
        <textarea id='t2'> 
        <input id="MyInput" type="submit">  
    </body>
    

    并在文件准备好后装订:

    $(document).ready(function(){
        $("#MyInput").click(updateparent());
    });
    
        2
  •  1
  •   AlfaTeK    15 年前

    一种更“jquery”的方法:

    page1.html
    ----------
    
    <script type="text/javascript">
    $(document).ready(function() {
    
        $("#link1").click(function(){
            window.open('page2.html','','width=600');    
        });
    });
    </script>
    <body> 
        <textarea id='t1'>
        <a id="link1" href="#">Click here</a> 
    
    </body>
    
    
    page2.html
    ----------
    <script type="text/javascript">
    $(document).ready(function() {
        $("#link1").click(function(){
            window.opener.jQuery("#t1").val($("#t2").val());    
            window.close();
        });
    
    });
    </script>
    <body> 
        <textarea id='t2'>
        <input type="submit" id="link2"> 
    </body>