如何防止 iframe 在 DOM 中移动时重新加载

发布于 2024-12-04 22:28:58 字数 161 浏览 1 评论 0原文

我有一个加载了一些内容的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野心澎湃 2024-12-11 22:28:58

如果您尝试以视觉方式移动它,可以尝试修改 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.

姐不稀罕 2024-12-11 22:28:58

我不这么认为,因为当 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.

夜未央樱花落 2024-12-11 22:28:58

我在 jQueryUI 对话框中的 iFrame 上遇到了类似的问题。 jQuery 将 div(包含我的 iFrame)移出了 DOM,并且因为我仍然想要回发(废话),所以我不得不将其移回来。阅读这篇文章和其他几篇文章后,我想出了一个解决方案。

我注意到的简单想法是 iFrame 在移动时会重新加载。因此,在将 div 移回 DOM 后,我将 iFrame 添加到对话框容器 (div) 中。这是可行的,因为 jQuery 不关心容器中的内容。这是一些示例代码:

对话框打开/关闭功能:

open:

function () {
    $(this).parent().appendTo("form").css("z-index", "9000"); //Move the div first
    $(this).append('<iframe id="iFrame" allowtransparency="true" frameborder="0" width="100%" height="100%" src="somePage.aspx"></iframe>');}, //then add the iframe
}

close:

function() {
    $(this).empty(); //clear the iframe out because it is added on open, 
    //if you don't clear it out, you will get multiple posts
    $(this).parent().appendTo("divHidden"); //move the dialog div back (good house-keeping)
}

html:

<div id="divHidden" style="display: none">
    <div id="dialog" style="display: none">
    </div>
</div>

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:

function () {
    $(this).parent().appendTo("form").css("z-index", "9000"); //Move the div first
    $(this).append('<iframe id="iFrame" allowtransparency="true" frameborder="0" width="100%" height="100%" src="somePage.aspx"></iframe>');}, //then add the iframe
}

close:

function() {
    $(this).empty(); //clear the iframe out because it is added on open, 
    //if you don't clear it out, you will get multiple posts
    $(this).parent().appendTo("divHidden"); //move the dialog div back (good house-keeping)
}

html:

<div id="divHidden" style="display: none">
    <div id="dialog" style="display: none">
    </div>
</div>
我要还你自由 2024-12-11 22:28:58

您需要将“Html”节点保存在 iframe 下,并在移动 iframe 后将其添加回来

you need to save the 'Html' node under the iframe and after you moved the iframe, add it back

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文