举例:
-----------------------a.html--------------------------
<html>
<Script>
document.write ("马上就会弹出一个小窗口~");
helloWin = open ('b.html','','height=100,width=300');
//helloWin.document.write ("啊呀呀啊呀呀~~");
helloWin.document.title = "欢迎光临";
</Script>
<body>
</body>
</html>
-----------------------a.html--------------------------
-----------------------b.html--------------------------
<html>
<script>
function changeParentBg(color){
opener.document.bgColor = color;
}
</script>
<body onclick = "self.close()">
<input type ="button" onclick = "changeParentBg('red')" value = "红色"/>
<input type ="button" onclick = "changeParentBg('blue')" value = "蓝色"/>
</body>
</html>
-----------------------b.html--------------------------
说明:上面的代码,如果把a.html中的write注释,那么b.html的onclick = "self.close()">方法就可以正常使用,否则无法使用self的close方法关闭窗口,并且b.html中的<input type ="button" onclick = "changeParentBg('red')" value = "红色"/>
<input type ="button" onclick = "changeParentBg('blue')" value = "蓝色"/>这些代码都无法显示
-----------------------a.html--------------------------
<html>
<Script>
document.write ("马上就会弹出一个小窗口~");
helloWin = open ('b.html','','height=100,width=300');
//helloWin.document.write ("啊呀呀啊呀呀~~");
helloWin.document.title = "欢迎光临";
</Script>
<body>
</body>
</html>
-----------------------a.html--------------------------
-----------------------b.html--------------------------
<html>
<script>
function changeParentBg(color){
opener.document.bgColor = color;
}
</script>
<body onclick = "self.close()">
<input type ="button" onclick = "changeParentBg('red')" value = "红色"/>
<input type ="button" onclick = "changeParentBg('blue')" value = "蓝色"/>
</body>
</html>
-----------------------b.html--------------------------
说明:上面的代码,如果把a.html中的write注释,那么b.html的onclick = "self.close()">方法就可以正常使用,否则无法使用self的close方法关闭窗口,并且b.html中的<input type ="button" onclick = "changeParentBg('red')" value = "红色"/>
<input type ="button" onclick = "changeParentBg('blue')" value = "蓝色"/>这些代码都无法显示
本文通过一个具体的示例展示了如何使用JavaScript来打开新窗口并实现窗口间的交互操作,包括改变父窗口背景颜色及关闭子窗口的功能。
1662

被折叠的 条评论
为什么被折叠?



