如何防止 iframe 在 DOM 中移动时重新加载
我有一个加载了一些内容的 iframe。我想将它移动到 DOM 内部而不引起刷新(我喜欢其中的内容,我想保留它)。
我正在做一些基本的 node.appendChild(iframe)
来完成这项工作。
这可能吗?
预先感谢您的帮助。
I have an iframe loaded with some content. I would like to move it inside the DOM without causing a refresh (I like the content inside it, I want to keep it).
I'm doing some basic node.appendChild(iframe)
to do the job.
Is that possible?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您尝试以视觉方式移动它,可以尝试修改 CSS 以使用绝对定位或其他一些调整。
但是,如果您尝试将其从 DOM 中实际拉出并将其插入其他位置,则将无法避免重新加载。
If you're trying to move it visually, you can try modifying the CSS to use absolute positioning or some other adjustments.
However, if you're trying to actually pull it out of the DOM and insert it somewhere else, you won't be able to avoid a reload.
我不这么认为,因为当 iframe 放回 DOM 中时,浏览器将重新渲染/重新加载 iframe。
http://polisick.com/moveNode.php 更好地解释了它。
要移动节点,您可以调用removeNode将其从树中取出并放入内存中,然后appendNode将其“粘贴”回您想要的位置。
I don't think so, as the browser is going to re-render/reload the iframe when it's put back in the DOM.
http://polisick.com/moveNode.php better explains it.
To move a node you call removeNode to take it out of the tree and into memory, then appendNode to 'paste' it back where you want it.
我在 jQueryUI 对话框中的 iFrame 上遇到了类似的问题。 jQuery 将 div(包含我的 iFrame)移出了 DOM,并且因为我仍然想要回发(废话),所以我不得不将其移回来。阅读这篇文章和其他几篇文章后,我想出了一个解决方案。
我注意到的简单想法是 iFrame 在移动时会重新加载。因此,在将 div 移回 DOM 后,我将 iFrame 添加到对话框容器 (div) 中。这是可行的,因为 jQuery 不关心容器中的内容。这是一些示例代码:
对话框打开/关闭功能:
open:
close:
html:
I had a similar problem with an iFrame in a jQueryUI dialog box. jQuery moves the div (containing my iFrame) out of the DOM and because I still wanted postbacks (duh), I had to move it back. After reading this and several other posts, I came up with a solution.
The simple idea that I noticed is that the iFrame reloads when it is moved. So, I added the iFrame into the dialog container (div) after moving the div back into the DOM. This works because jQuery doesn't care about what's in the container. Here is some sample code:
Dialog open/close functions:
open:
close:
html:
您需要将“Html”节点保存在 iframe 下,并在移动 iframe 后将其添加回来
you need to save the 'Html' node under the iframe and after you moved the iframe, add it back