JavaScript访问Firefox中的iframe内部变量

时间:2013-10-31 12:53:13

标签: javascript variables firefox dom iframe

以下适用于Internet Explorer和Chrome,但不适用于Firefox 我们的想法是在iframe中每次点击后更改父体的背景 请注意,Firefox最初会知道设置蓝色背景。

的index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><script>
  function polling() {
    if (window.iframeId.change) document.body.style.background = 'red';
    if (!window.iframeId.change) document.body.style.background = 'blue';
    setTimeout(polling, 1000); }
</script></head>
  <body onload="polling();">
    <iframe id="iframeId" src="frame.html" style="width:100px; height:100px;"></iframe>
  </body>
</html>

frame.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head><script>var change=false;</script></head>
  <body style="background:yellow;" onclick="change=!change;"></body>
</html>

1 个答案:

答案 0 :(得分:1)

这就是我的工作方式:

<强>的index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <script>
        function polling() {
            if (window.frames['theIframe'].change) {
                document.body.style.background = 'red';
            }
            else {
                document.body.style.background = 'blue';
            }

            setTimeout(polling, 1000);
        }
    </script>
</head>
<body onload="polling();">
    <iframe id="iframeId" src="frame.html" name="theIframe" style="width: 100px; height: 100px;"></iframe>
</body>
</html>

<强> frame.html

<!DOCTYPE HTML >
<html>
<head>
    <script>
        var change = false;
        function load() {
            document.onclick = doClick;
        }        
        function doClick() {
            change = !change;
        }

    </script>
</head>
<body style="background: yellow;"  onload="load()"></body>
</html>