Javascript 从iFrame中的按钮关闭(对话框)iFrame

Javascript 从iFrame中的按钮关闭(对话框)iFrame,javascript,html,css,Javascript,Html,Css,我有两个文件,分别名为main\u page.html和dialog\u box.html(参见下面的代码)。 我想让我的handleCloseDialogButton()关闭对话框,换言之,重新加载main\u page.html,并将frame.style.display重置为“无”。 我该怎么做?我应该使用window.open(“main_page.html”)还是window.location.href=“main_page.html”还是其他什么? 注意:我不想使用Javascrip

我有两个文件,分别名为
main\u page.html
dialog\u box.html
(参见下面的代码)。 我想让我的
handleCloseDialogButton()
关闭对话框,换言之,重新加载
main\u page.html
,并将frame.style.display重置为“无”。 我该怎么做?我应该使用
window.open(“main_page.html”)
还是
window.location.href=“main_page.html”
还是其他什么? 注意:我不想使用Javascript的
alert()
函数,因为它的功能不足以满足我的需要

main_page.html的内容:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<script>
function handleDialogButton()
{
var theFrame=document.getElementById("myDialogBox");
theFrame.style.display="block";
theFrame;
}
</script>
</head>
<body>
<div id="myDiv"> <h2> Hello world. This is the main page.</h2></div>
<iframe  id="myDialogBox" src="./dialog_box.html" style="display:none"> </iframe>
<input type="button" id="dbbutton" name="dbbutton" value="Open Dialog Box" 
onClick="javascript:handleDialogButton();" /> 
</body>
</html>

函数handleDialogButton()
{
var theFrame=document.getElementById(“myDialogBox”);
theFrame.style.display=“block”;
框架;
}
你好,世界。这是主页。
dialog_box.html的内容:

<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<script>
function handleCloseDialogButton()
{

}
</script>
</head>
<body>
<div id="myDiv"> <h2> Hello world. I am the dialog box.</h2></div>
<input type="button" id="cbbutton" name="cbbutton" value="Close Dialog Box" 
onClick="javascript:handleCloseDialogButton();" /> 
</body>
</html>

函数handleCloseDialogButton()
{
}
你好,世界。我正在打开对话框。

您可以使用window.parent从iframe访问父元素。 有关详细信息,请参阅

使用window.parent,您可以调用父js函数,也可以访问父元素。在您的案例中访问父元素应该如下所示:window.parent.document.getElementById('myDialogBox')

在handleCloseDialogButton函数中使用此选项,并将其显示设置为无:

function handleCloseDialogButton()
{
window.parent.document.getElementById('myDialogBox').style.display="none";
}

注意:这在Chrome的文件模式下不起作用。你们必须把你们的网页放在网络服务器上,然后在Chrome浏览器上也能工作

window.location.href=“main\u page.html”
就足够了。您还可以重置div:
document.getElementById(“myDialogBox”).style.display=“none”
@devqon您的建议无效。它只是在对话框中显示主页内容。我明白了,你的问题没有读对。您的意思是从iFrame中的按钮关闭iFrame。@devqon编辑了标题,谢谢