Javascript iframe外部的getElementById
我在尝试引用预加载iframe之外的ID时遇到问题。我想知道是否可以引用iframe加载页面上的ID,例如:Javascript iframe外部的getElementById,javascript,iframe,Javascript,Iframe,我在尝试引用预加载iframe之外的ID时遇到问题。我想知道是否可以引用iframe加载页面上的ID,例如: <html> <body> <div id="username">Guest</div> <iframe src="/file.html"> </body> </html> 客人 在file.html中,有一个简单的脚本可以尝试检查“username”的innerHTML是什么: &
<html>
<body>
<div id="username">Guest</div>
<iframe src="/file.html">
</body>
</html>
客人
在file.html中,有一个简单的脚本可以尝试检查“username”的innerHTML是什么:
<html>
<body>
<div id="result">Empty</div>
<button id="btn" type="button">Click</button>
<script>
document.getElementById('btn').onclick = function() {
document.getElementById('result').innerHTML = document.getElementById('username').innerHTML;
}
</script>
</body>
</html>
空的
点击
document.getElementById('btn')。onclick=function(){
document.getElementById('result').innerHTML=document.getElementById('username').innerHTML;
}
已经尝试了几种不同的方法,但都不起作用。我非常感谢您帮助我解决这个问题,或者至少知道这是否可行。在
iframe
中添加id
,然后:
document.getElementById(“iframe的id”).getElementById(“btn”)代码>在iframe中使用window.parent.document.getElementById()
与父文档交互 在iframe中使用window.parent.document.getElementById()
与父文档交互。真不敢相信这么简单。非常感谢你。