Take a look at this simple HTML:

<div id="wrap1">
  <iframe id="iframe1"></iframe>
</div>
<div id="warp2">
  <iframe id="iframe2"></iframe>
</div>

比如说,我想移动包裹,使#wrap2#wrap1早.iFrame被JavaScript污染了.我知道jQuery的.insertAfter().insertBefore().然而,当我使用它们时,iFrame会丢失所有的HTML、JavaScript变量和事件.

假设以下是iFrame的HTML:

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      // The variable below would change on click
      // This represents changes on variables after the code is loaded
      // These changes should remain after the iFrame is moved
      variableThatChanges = false;
      $(function(){
        $("body").click(function(){ 
          variableThatChanges = true; 
        });
      });
    </script>
  </head>
  <body>
    <div id='anything'>Illustrative Example</div>
  </body>
</html>

In the above code, the variable variableThatChanges would...change if the user clicked on the body. This variable, and the click event, should remain after the iFrame is moved (along with any other variables/events that have been started)

我的问题如下:with JavaScript (with or without jQuery), how can I move the wrap nodes in the DOM (and their iframe childs) so that the iFrame's window stays the same, and the iFrame's events/variables/etc stay the same?

推荐答案

如果不重新加载iframe,则无法将其从dom中的一个位置移动到另一个位置.

下面的示例说明即使使用原生JavaScript,iFrame仍会重新加载: http://jsfiddle.net/pZ23B/

var wrap1 = document.getElementById('wrap1');
var wrap2 = document.getElementById('wrap2');
setTimeout(function(){
    document.getElementsByTagName('body')[0].appendChild(wrap1);
},10000);

Jquery相关问答推荐

使用jquery ui来回滑动切换

在 Laravel 中使用 jQuery post 按相关值过滤 Select 选项,如何显示从控制器返回的数据?

添加动态表单后,Select2 消失

JQuery:$.get 不是函数

如何在 jquery 中包含 !important

jquery如何捕获输入键并将事件更改为选项卡

Jquery Ajax,从 mvc.net 控制器返回成功/错误

jQuery: Select 属性大于值的所有元素

如何在循环内创建动态变量名称?

使用 jQuery click 处理锚点 onClick()

一次替换多个字符串

lodash debounce 在匿名函数中不起作用

jQuery 如果 div 包含此文本,则替换该部分文本

如何使用 JSON、jQuery 将一组复杂对象发布到 ASP.NET MVC 控制器?

延迟jquery悬停事件?

如何在 JavaScript 中的对象数组中查找值?

jQuery位置href

将数据发布到 JsonP

如何插入元素作为第一个子元素?

是否可以将 async:false 设置为 $.getJSON 调用