gpt4 book ai didi

javascript - 从 iframe 访问和更改父页面(使用 jquery)

转载 作者:技术小花猫 更新时间:2023-10-29 12:04:28 26 4
gpt4 key购买 nike

有什么方法可以从 iframe 访问父页面(并更改父页面)?

<body>
<iframe src="frame1.html" name="frame1" height="100%"></iframe>
<div id="test1"></div>
</body>

在 frame1.html 中是 <a href=..>我想在 <h1>clicked</h1> 中添加文本“<div id="test1"></div> ” , 当<a href..>被点击了。

谢谢。

最佳答案

如果您的子页面(frame1.html)与父页面位于同一域,您可以在子窗口中编写如下代码:

 $('#test1', parent.document).html('<h1>clicked</h1>');

第二个参数提供上下文,在其中搜索与第一个参数匹配的元素。文件在这里:http://api.jquery.com/jQuery/#jQuery-selector-context

 jQuery( selector [, context ] )

因此,您的代码 (frame1.html) 可以像这样:

 <a href="..." 
onclick="$('#test1', parent.document).html('<h1>clicked</h1>');">click me</a>

希望这对您有所帮助。

关于javascript - 从 iframe 访问和更改父页面(使用 jquery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10748449/

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