繁体   English   中英

此代码段的JQuery代码是什么?

[英]What is the JQuery code for this snippet?

我有一个父页面,其中包含一个文本区域和一个打开子窗口的链接。 子窗口具有另一个文本区域和一个按钮。 用户在子窗口的文本区域中输入一些文本,然后单击按钮,将触发一个javascript,该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>

有趣的问题。

正如Domonic Roger所提到的,如果它起作用了,那么您可能想离开它。

对我来说,问题不是“此代码段的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 UI中可用的弹出窗口功能或某些插件(例如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());
});

一种更“ 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM