簡體   English   中英

當子iframe獲得焦點時,防止文檔丟失鍵綁定

[英]Prevent document from losing key bind when a child iframe takes focus

基本上我的目標是嘗試為我的整個Web項目創建一個鍵盤控制器,我希望能夠保留我在父文檔上創建的鍵綁定,並讓它們由子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");
    }
});

我希望能夠讓tab鍵仍然綁定,即使iframe獲得焦點,我意識到我可以將tab鍵重新綁定到iframe文檔,但有沒有辦法避免這種情況?

在您的iframe中,您可以添加一個觸發top事件的事件處理程序......將這部分代碼放在iframe文檔中:

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

這應該簡單地將事件“轉發”到頂部文檔。 請參閱http://api.jquery.com/category/events/event-object/和jap API中的http://api.jquery.com/trigger/ ,以便能夠稍微播放一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM