猿问

iframe 问题

现在的问题是 ,网站是用iframe写的,分为top,left,right三个部分,现在我想在top页面执行一个方法,然后在right页面弹出一个窗口,怎么实现呢?

阿晨1998
浏览 419回答 1
1回答

翻过高山走不出你

题主的问题本质是:iframe子页面内执行另外一个iframe子页面的javascript方法我贴代码:main.html 主窗口<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <iframe name="i1" id="i1" src="1.html" height="300" width="100%"></iframe>&nbsp; &nbsp; &nbsp; &nbsp; <iframe name="i2" id="i2" src="2.html" height="300" width="100%"></iframe>&nbsp; &nbsp; </body></html>i1.html 是你的top页面<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h2>这是top iframe</h2>&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" onclick="rightShow()">执行Right弹窗方法</button>&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function rightShow(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取父窗口顶层对象&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parent = window.parent;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 获取right窗口对象&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $right = $parent.document.getElementById("i2").contentWindow;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 执行right窗口弹窗方法&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $right.showWindow();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>i2.html 是你的right页面<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; &nbsp; &nbsp; <title></title>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h3>这是right iframe</h3>&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 这就是right的弹窗方法&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function showWindow() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('window shown!');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答