0

我在 index.html 上有这段代码

<body>
    <iframe id=iframetest src="alt.html">
        <p>Your browser does not support iframes.</p>
    </iframe>
    <script>
        $(window).load(function(){
             document.getElementById('iframetest').contentWindow.myFunction();
        });
    </script>
    <button onclick="myFunction();">Click Me</button>
</body>

这个 1 在 alt.html 中

<head>
    <title>JavaScript Test</title>
    <script type="text/javascript">
        //When click on the button function will run
        function myFunction(){
        alert("Hello World");
        }
    </script>
</head>
<body>
    <h1>TEST</h1>
</body>

我想在 index.html 中显示警报。但这不起作用。我该怎么做?请帮助我

谢谢你!

4

1 回答 1

3

这被认为更可靠并且可能有效:

<body>
    <iframe name=iframetest src="alt.html">
        <p>Your browser does not support iframes.</p>
    </iframe>
    <script>
        $(window).load(function(){
             window.frames['iframetest'].myFunction();
        });
    </script>
    <button onclick="myFunction();">Click Me</button>
</body>

这是一个可以解释它的链接。

还要确保 alt 和 index.html 使用相同的协议(HTTP 或 HTTPS)在同一个域中

于 2013-11-07T11:45:02.470 回答