javascript 如何访问属于另一个html页面的元素ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6973548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:32:32  来源:igfitidea点击:

how to access element id which belongs to another html page?

javascripthtml

提问by s.onal

i have a javascript problem

我有一个 javascript 问题

there are three html page like 'page1.html', 'anotherPage.html' and anotherPage.html

有三个 html 页面,如“page1.html”、“anotherPage.html”和 anotherPage.html

FOR page1.html

对于 page1.html

<div id='mainFrame'></div>

<ul><li onclick="updateFrame(id,"anotherPage.html");"><a href="javascript:;"></a></li></ul>


FOR anotherPage.html

FOR anotherPage.html

<ul><li onclick="updateFrame(id,anotherPage2);"><a href="javascript:;"></a></li></ul>


my javascript function:

我的 javascript 函数:

function updateFrame(id,url){
document.getElementById(id).innerHTML="<iframe scrolling='no' style='height:2000px;position:absolute;width:100%;height:100%;left:10px;border-style:none' src=\" "+ url+ "\"></iframe>";} 


The problem is that when i called java script in iframe, javascript did not find the parent document id. How can i access the parent document id ? (Parent document is another html page in this case)

问题是当我在 iframe 中调用 java 脚本时,javascript 没有找到父文档 ID。如何访问父文档 ID?(在这种情况下,父文档是另一个 html 页面)

Thanks.

谢谢。

采纳答案by Dennis Traub

parent.document.getElementById('myControlId')