<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>04.子页面调用主页面方法</title>
<script>
/* https://blog.csdn.net/qq_27626333/article/details/77990196
* 在iframe本页面,要操作这个iframe的父页面的DOM元素(即嵌套这个iframe的页面)
* 可以用:window.parent、window.top(这里的TOP是获取的顶层,即有多层嵌套iframe的时候使用)
* */
function parentValue() {
/* 被 子页面 调用。*/
if (confirm('欢迎访问:http://blog.csdn.net/qq_27626333')) {
location.href = 'http://blog.csdn.net/qq_27626333'
}
}
</script>
</head>
<body>
<iframe id="myFrame" src="05.childWeb.html" width="510px" height="150px;"></iframe>
</body>
</html>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<title>05.childWeb</title>
<style>
.contain {
display: flex;
flex-direction: column;
/*flex-direction: row;*/
width: 500px;
}
#test {
border: solid royalblue 2px;
border-radius: 4px;
width: 100%;
color: #797979;
height: 50px;
line-height: 50px;
font-size: 20px;
}
#button {
border: solid #20a0ff 2px;
border-radius: 4px;
margin: 20px auto;
padding: 10px;
background-color: #20a0ff;
color: white;
}
</style>
<script>
function parentValue() {
/* 在子页面中,调用主页面(父页面)的方法。*/
window.parent.parentValue()
}
</script>
</head>
<body>
<div class="contain">
<input
type="text"
id="test"
value="欢迎访问:http://blog.csdn.net/qq_27626333"
/>
<input
type="button"
id="button"
value="调用主页面方法"
onclick="parentValue()"
/>
</div>
</body>
</html>