jQuery sweetalert2 打开时,keydown 事件不起作用

jQuery keydown event not working when sweetalert2 is open

我的页面上有一个 keydown 事件,但是当 sweetalert 打开时 keydown 事件没有触发。

$(() => {
    swal.fire({
        icon: "error",
        title: 'Please wait...',
        html: 'Trying again in <b>10</b> seconds.',
        timer: 10000,
        timerProgressBar: true,
        didOpen: () => {
          swal.showLoading()
          timerInterval = setInterval(() => {
            const content = swal.getContent()
            if (content) {
              const b = content.querySelector('b')
              if (b) {
                b.textContent = Math.ceil(swal.getTimerLeft() / 1000);
              }
            }
          }, 1000)
        },
        willClose: () => {
          clearInterval(timerInterval)
        }
      }).then((result) => {
        if (result.dismiss === swal.DismissReason.timer) {
          console.log("finished");
        }
      });
});

$(document).on("keydown", function (e) {
  if (e.key.charCodeAt() == 83 || e.key.charCodeAt() == 115) {
    swal.close();
    console.log("interrupted");
  }
});

有关示例,请参见此 jsfiddle。虽然 sweetalert 是 运行,但按“S”键不会执行任何操作,但是如果您关闭 sweetalert,则 keydown 会触发。

https://jsfiddle.net/nk087u2s/9/

来自文档

stopKeydownPropagation: true

If set to false, SweetAlert2 will allow keydown events propagation to the document.

$(() => {
  swal.fire({
    stopKeydownPropagation: false,