gpt4 book ai didi

javascript - 将 Iframe 添加到文档时,如何防止重新加载 Iframe?

转载 作者:行者123 更新时间:2023-11-30 06:05:11 24 4
gpt4 key购买 nike

我们正在使用 simplemodal jquery 插件来托管一个 iframe 作为对话框的内容。关闭对话框后,simplemodal 从文档中删除对话框内容(包裹在 div 中的 iframe),然后将其添加回文档。

以下标记在将 simplemodal 排除在等式之外时演示了问题。我在这篇文章中仅提及 simplemodal 以了解为什么删除并重新添加 iframe 的上下文。

如何防止 Iframe 在重新添加到文档时重新加载其内容?

如有任何帮助,我们将不胜感激!

测试.html


<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
iframe{ padding: 0px; margin: 0px; width: 300; height: 300; }
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#go").click(function() {
var frame = $("#myframe");

frame.remove();
frame.prependTo("body");
});
});
</script>
</head>
<body>
<iframe name="myframe" id="myframe" src="test2.html"></iframe>
<div>
<button id="go">GO</button>
</div>
</body>
</html>

test2.html


<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
alert("this should not get called when the iframe is re-added to the document");
});
</script>
</head>
<body>
This is iframe content.
</body>
</html>

最佳答案

我认为如果使用 remove() 是不可能的。但也许您可以尝试其他方式,例如更改其大小、显示:无、不透明度:0、可见性:隐藏或其他。你试过 detach() 吗?在 jQuery 文档中说:要在不删除数据和事件的情况下删除元素,请改用 .detach()。

关于javascript - 将 Iframe 添加到文档时,如何防止重新加载 Iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5396769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com