【问题标题】:jquery re-attach div on pop up window closejquery在弹出窗口关闭时重新附加div
【发布时间】:2013-08-14 18:10:03
【问题描述】:

我正在使用 jQuery 打开一个弹出窗口,效果很好,但我也有 #content div 与“父”页面分离...

$('.newWindow').click(function(ev){

window.open('popout.html','pop out','width=400,height=345');
ev.preventDefault();
$('#content').detach();
return false;

});

使用的链接:

<a href="popout.html" rel="0" class="newWindow">Pop Out Window</a>

当弹出窗口关闭时,如何“重新附加”该 div?

我已经尝试了许多我在 SO 上找到的答案,但似乎都没有。

更新:

当我查看源代码时(通过右键单击查看源代码),我的浏览器似乎缓存了弹出窗口...

暂时去了:

window.onunload = function(){
  window.opener.location.reload();
};

在子关闭时刷新父页面,但仍然更喜欢重新附加方法。

【问题讨论】:

    标签: jquery popupwindow


    【解决方案1】:

    你可以试试

    var p; // where p may be of global scope
    
    $('.newWindow').click(function(ev){
    
    window.open('popout.html','pop out','width=400,height=345');
    ev.preventDefault();
     p = $('#content').detach();
    return false;
    });
    

    //关闭窗口

    if(p){
     p.appendTo("body");//or where ever you want it to append
     p = null;
    }
    

    代码取自给定的示例here

    【讨论】:

      【解决方案2】:

      创建一个临时变量来存储detaching之前的div。

      var content; // temp storage
      
      $('.newWindow').click(function(ev){
      
           window.open('popout.html','pop out','width=400,height=345');
           ev.preventDefault();
           content = $('#content');
           content.detach();
           return false;
      });
      

      当您需要再次在 DOM 中重新附加它时,只需使用 .appendTo()

      content.appendTo($("#whereToReAttach"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-15
        相关资源
        最近更新 更多