gpt4 book ai didi

jquery - iframe 和它们之间的通信

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:42 27 4
gpt4 key购买 nike

我有一个页面,索引,具有以下内容和 iframe 设置:

索引页:

<html>
<body>

<iframe src=".." id="leftFrame"></frame>
<iframe src="mainFrame.html" id="mainFrame"></frame>

</body>

</html>

mainFrame.html:

<div class="hidden" id="hiddenStuff">
....
</div>

现在在 mainFrame 中我正在一个隐藏的 div 中加载一些内容。

是否可以从 mainFrame.html 获取该内容并将其显示在索引中,通过 javascript 从 mainFrame 触发

即主框架加载后,它将通过 javascript 调用父 iframe 以获取#hiddenStuff 的内容,然后将其显示在索引页面中。

最佳答案

是的,如果父框架和 mainFrame 都在同一个域中,这是可能的。

父级/索引:

<html>
<body>

<script type="text/javascript">

function updateContent(content)
{
$('#content').html(content);
}

document.domain = "yourdomain.com";

</script>

<iframe src=".." id="leftFrame"></frame>
<iframe src="mainFrame.html" id="mainFrame"></frame>

<div id="content"> </div>

</body>

</html>

主框架/子框架:

<script type="text/javascript">

document.domain = "yourdomain.com";

$(document).ready(function(){

// on page load, trigger the update
var content = $('#hiddenStuff').html();

try
{
parent.updateContent(content);
}
catch(e)
{
// couldn't call the parent, handle the exception
}

});

</script>

<div class="hidden" id="hiddenStuff">
....
</div>

请记住,在将原始 HTML 处理成元素时,如果任何内容来自用户输入,您需要考虑 XSS 的可能性。

关于jquery - iframe 和它们之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983071/

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