最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to stop TAB key event from firing when a modal is open - Stack Overflow

programmeradmin0浏览0评论

When the modal is showing, how can I disable key events like TAB? I have multiple buttons and fields in a form on the page that can be selected (focused) by tabbing. I want to disable that when the modal is showing. The application uses Angular and the modal is in a child ponent.

<button tabindex="0">Button1</button>
<button tabindex="0">Button2</button>
<form>...</form>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">...</div>
  </div>
</div>

When the modal is showing, how can I disable key events like TAB? I have multiple buttons and fields in a form on the page that can be selected (focused) by tabbing. I want to disable that when the modal is showing. The application uses Angular and the modal is in a child ponent.

<button tabindex="0">Button1</button>
<button tabindex="0">Button2</button>
<form>...</form>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">...</div>
  </div>
</div>
Share Improve this question asked Jan 22, 2018 at 13:23 matchimatchi 5933 gold badges6 silver badges19 bronze badges 2
  • Possible duplicate of Keep tabbing within modal pane only – user555121 Commented Jan 22, 2018 at 13:29
  • @Kos, I'm looking for an Angular typescript solution rather than javascript. So that might be different – matchi Commented Jan 22, 2018 at 13:34
Add a ment  | 

4 Answers 4

Reset to default 5

You can add your own event listener:

// Your event listener
function preventTab(e) {
  e = e || window.event;
  if (e.keyCode === 9) { // If tab key is pressed
    e.preventDefault() // Stop event from its action
  }
}

// Call this when modal window opens
document.addEventListener(preventTab);

// Call this when modal window closes/unmounts
document.removeEventListener(preventTab);

I'd suggest something like this - use Renderer2 and listen for a keydown event and filter events by keyCode. Then on modal hide remove that listener.

onShow() {
   this.removeTabKeyListener = this.renderer.listen('document', 'keydown', (event) => {
      if (event.keyCode === 9) { 
        event.preventDefault();
      }
    });
}

onHide() {
  this.removeTabKeyListener();
}

A little example - http://plnkr.co/edit/LdpmCpgapPbrA26fGO9U?p=preview

You can disable tab by preventDafault the keyCode 9 (tab) on keydow:

$(document).keydown(function(e) {
    if (e.keyCode == 9) {
        e.preventDefault();
    }
});

Of course you'll want that on your modal opener and not as is, so it won't permanently disable tabs ;)

Try this code it will just disable your tab key when modal is showing.

 $(document).keydown(function(e){
    if (e.which==9) // keycode for tab key
    {
        checkmodal = $('#myModal').is(':visible'); //check modal is opened or not
        if (checkmodal){
            e.preventDefault();
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论