javascript - 使用 jquery 从 iframe 更新父级

标签 javascript jquery html iframe

我有这个最小的测试用例

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>

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>

这里的目的是在点击 iframe 上的按钮时更新父窗口中的输入框。正如您在上面看到的那样,我尝试使用纯 javascript 来完成它,但它也不起作用。如果可能的话,我想使用 jquery 来做到这一点。

最佳答案

function update_parent(value) {
    $('#myinput', window.parent.document).val(value);
}

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

关于javascript - 使用 jquery 从 iframe 更新父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795452/

相关文章:

javascript - 纯粹与 UI 相关的 javascript 在 Durandal View 中放在哪里?

javascript - 无法在 safari 上的 onsubmit 表单事件中使用 javascript 加载图像

JavaScript 表单验证 2 个字母 1 个数字

javascript - 由于点 ID 导致 JSON 调用出错

php - JQuery 删除和添加类

JavaScript/AngularJS : Check if an element has the style overflow-y applied to it

jquery - 尝试使用 knockoutjs 自动刷新 JSON

jquery - 自动滑动 JQUERY

javascript - 将滚动条添加到动态生成的选择选项

html - 带有子菜单的移动菜单 - 当 <li> 中有一个打开子菜单的按钮时,如何使整个 <li> 标记包裹在 <a> 标记中