<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>js iframe</title><script>/*参考:/qq_27626333/article/details/77990196* 知识点:* 在iframe本页面,要操作这个iframe的父页面的DOM元素(即嵌套这个iframe的页面)* 可以用:window.parent、window.top(这里的TOP是获取的顶层,即有多层嵌套iframe的时候使用)* */function parentValue() {/*功能:被 子页面 调用。*/if (confirm('欢迎访问:/qq_27626333')) {location.href = '/qq_27626333'}}</script></head><body><iframe id="myFrame" src='cs3.html' width="510px" height="150px;"></iframe></body></html>
// cs3.html<!doctype html><html lang="en"><head><meta charset="UTF-8"><title>js iframe.contentWindow</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='欢迎访问:/qq_27626333'><input type="button" id="button" value="调用主页面方法" οnclick="parentValue()"/></div></body></html>