gpt4 book ai didi

javascript - 当子 iframe 获得焦点时,防止文档丢失键绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 18:29:02 27 4
gpt4 key购买 nike

基本上,我的目标是尝试为整个 Web 项目创建一个键盘 Controller ,并且我希望能够保留在父文档上创建的键绑定(bind),并让子 iframe 继承它们。这可能吗?

这是我现在拥有的示例设置:http://jsfiddle.net/sahil_signal/d39Nn/9/

<div id="tabcontainer">
<button id="tab1button" type="button">Tab1</button>
<button id="tab2button" type="button">Tab2</button>
</div>
<iframe id="iframe1container"></iframe>
<iframe id="iframe2container"></iframe>

$(document).on('keydown', function (e) {
var keycode = e.keyCode || e.which;

if (keycode == 9) {
e.preventDefault();
console.log("Tab");
}
});

即使 iframe 获得焦点,我也希望能够仍然绑定(bind) Tab 键,并且我意识到我可以将 Tab 键重新绑定(bind)到 iframe 文档,但有没有办法避免这种情况?

最佳答案

在您的 iframe 中,您可以添加一个事件处理程序来触发 top 的事件...将这部分代码放入您的 iframe 文档中:

$(document).on('keydown', function (e) {
$(top.document).trigger('keydown',e);
});

这应该只是将事件“转发”到顶级文档。请参阅http://api.jquery.com/category/events/event-object/http://api.jquery.com/trigger/从 jQuery API 就可以围绕它进行一些操作。

关于javascript - 当子 iframe 获得焦点时,防止文档丢失键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com