简体   繁体   English

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

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

I have a parent page that contains a textarea and a link to open a child window. 我有一个父页面,其中包含一个文本区域和一个打开子窗口的链接。 The child window has another textarea and a button. 子窗口具有另一个文本区域和一个按钮。 User enters some text in the textarea in the child window and clicks the button, a javascript gets fired that updates the content of the textarea in the parent window with that of the textarea of the child window and closed the child window. 用户在子窗口的文本区域中输入一些文本,然后单击按钮,将触发一个javascript,该javascript用子窗口的文本区域的内容更新父窗口中文本区域的内容,并关闭子窗口。

I am currently doing this in javascript and it works fine, but since we will move to jQuery very soon how would one accomplish the same using jQuery. 我目前正在用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>

Interesting question. 有趣的问题。

As mentioned by Domonic Roger, if its working then you probably want to be leaving it. 正如Domonic Roger所提到的,如果它起作用了,那么您可能想离开它。

For me the question is not "What is the JQuery code for this snippet?", but how to I use jQuery to achieve the same solution. 对我来说,问题不是“此代码段的JQuery代码是什么?”,而是我如何使用jQuery实现相同的解决方案。

Sometimes it is not just a simple case of a straight code replace. 有时,这不只是简单的代码替换的简单情况。 Take the following: 采取以下措施:

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

Now, the jQuery code might be something like this: 现在,jQuery代码可能是这样的:

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

But, I wouldn't do this. 但是,我不会这样做。 I would use pop window functionality available in the jQuery UI, or some plugin (eg blockui ) to acheve the popup window. 我将使用jQuery UI中可用的弹出窗口功能或某些插件(例如blockui )来实现弹出窗口。

Also, this code: 另外,此代码:

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

In jQuery we are encouraged to use late binding of events, so any inline JavaScript would go: 在jQuery中,我们鼓励使用事件的后期绑定,因此任何内联JavaScript都可以使用:

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

And be bound once the document is ready: 准备好文档后,将其绑定:

$(document).ready(function(){
    $("#MyInput").click(updateparent());
});

A more "jquery" way to do this: 一种更“ 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