html 帧的复选框标签

Label for checkbox of an html frame

我该怎么做,例如,如果我们点击标签文本,我想切换(check/uncheck)box.html 的复选框页。

我已将 box.html 页面包含在 iframe 中。

Index.html

<html>
    <body>
        <iframe src="box.html" height="25px" width="100px">

        </iframe>
        <label for="box">
            checkbox
        </label>
    </body>
</html>

Box.html:

<html>
    <body>
        <input type="checkbox" id="box">
    </body>
</html>

首先,两个页面(Main 和 iframe)应该来自同一个域,否则会抛出跨域错误。

第 1 页

<html><head>
<script>
    function delegate() {
        var iframe = document.getElementById("myIframe");
        iframe.contentWindow.change();
    }
</script></head><body>
<iframe id="myIframe" src="box.html" height="25px" width="100px"></iframe>
<label for="box" onclick="delegate()">
    Click Here.
</label></body></html>

第 2 页

<html><head>
<script>
    function change() {
        document.getElementById('box').checked = !document.getElementById('box').checked;
    }
</script></head><body>
<input type="checkbox" id="box"></body></html>