前端程式碼集合(持續更新)

寒青發表於2021-10-09

按鍵處理方法

window.addEventListener("keydown", (e) => {
  let container = document.querySelector("#container");
  let { key, keyCode, code } = e;
  let template = "";
  [
    {
      title: "e.key",
      content: key == " " ? "Space" : key,
    },
    {
      title: "e.keyCode",
      content: keyCode,
    },
    {
      title: "e.code",
      content: code,
    },
  ].forEach((item) => {
    template += `<div class="key"><small>${item.title}</small>${item.content}</div>`;
  });
  container.innerHTML = template;
});

相關文章