Bootstrap 只需单击一个按钮即可打开modal / offcanvas

hgtggwj0  于 5个月前  发布在  Bootstrap
关注(0)|答案(1)|浏览(52)

我想用键盘上的一次点击打开我的收藏夹侧边栏。所以当按“f”时,我想打开侧边栏模式。现在它像这样打开:

<a href="#" data-bs-toggle="offcanvas" data-bs-target="#favorites">
    Open Favorites
</a>

字符串
我不知道如何用jquery做到这一点,我一直在尝试这样做,但没有成功:

$(document).keydown(function(e) {
 if (e.keyCode == "s") {
   $('#favorites').show();
 }
});

50pmv0ei

50pmv0ei1#

要在JavaScript中显示/隐藏offcanvas,您可以使用offcanvas示例,例如:const bsOffcanvas = new bootstrap.Offcanvas('#favorites');。它具有hideshowtoggle等方法。此外,f的keyCode为70。因此,当关键代码为70时,您可以使用bsOffcanvas.show();显示offcanas。参见以下示例:

$(document).keydown(function(e) {
  console.log(e.keyCode);
  if (e.keyCode == 70) {
    const bsOffcanvas = new bootstrap.Offcanvas('#favorites');
    bsOffcanvas.show();
  }
});

个字符

相关问题