使用jquery从iframe更新父级

yas*_*sar 3 html javascript iframe jquery

我有这个最小的测试用例

a.html

<!DOCTYPE html>
<html>
<body>
    <input type="text" id="myinput">
     <iframe id="frame" src="b.html" style="width:100%;height:100%" frameBorder="0"></iframe>
     </body>
</html>
Run Code Online (Sandbox Code Playgroud)

b.html

<!DOCTYPE html>
<html>

<body>
    <script>
        function update_parent(value) {
            window.parent.document.getElementById('myinput').value=value;
        }
    </script>
    <a class="tag" name="something" href="javascript:void(0)" onclick="update_parent(something)"/>Bla Bla</a>
    </body>

</html>
Run Code Online (Sandbox Code Playgroud)

这里的目的是在iframe上单击按钮时更新父窗口中的输入框.我尝试使用普通的javascript来做,如上所示,但它也不起作用.如果可能的话,我想使用jquery来做这件事.

Bri*_*pin 11

function update_parent(value) {
    $('#myinput', window.parent.document).val(value);
}
Run Code Online (Sandbox Code Playgroud)

$()包装器的第二个参数是要搜索的上下文.这默认为文档,因此传递父项将父项设置为搜索上下文.