简体   繁体   English

.keypress在浏览器中不起作用(Chrome浏览器除外)

[英].keypress not working in browsers (except chrome)

I have an js that is not working in any browser except chrome? 我有一个js,除了chrome以外,无法在任何浏览器中正常工作? Is it maybe the .keydown function that makes trouble. 可能是.keydown函数造成了麻烦。 There is a snippet of the code. 有一段代码。 It is made as an interactive video and when key is pressed a different layer of video is supposed to pop up. 它是作为交互式视频制作的,当按下按键时,应该弹出另一层视频。

    var videos = document.querySelectorAll("video");
var promises = Promise.all(Array.prototype.slice.call(videos).map(function(video) {
    return new Promise(function(resolve, reject) {
      video.addEventListener("canplaythrough", resolve);
      video.addEventListener("error", reject);
    })
  }))
  .then(function() {
    videos.forEach(function(video) {
      video.play();
    });
    videos[2].style.display = "none";
    document.addEventListener("keydown", function(e) {
      var key = e.key;
      if (key === "b" || key === "B") {
        videos[2].style.display = "block";
        videos[1].style.display = "none";
        videos[0].style.display = "none";
      }
    });
    videos[1].style.display = "none";
    document.addEventListener("keydown", function(e) {
      var key = e.key;
      if (key === "a" || key === "A") {
        videos[2].style.display = "none";
        videos[1].style.display = "block";
        videos[0].style.display = "none";
      }
    });
    document.addEventListener("keyup", function(e) {
      videos[2].style.display = "none";
      videos[1].style.display = "none";
      videos[0].style.display = "block";
    });
    window.focus();
  })
  .catch(function(err) {
    console.log(err);
  });

Browser doesn't give any errors. 浏览器没有给出任何错误。 And i don't know where to go on from here. 而且我不知道从这里继续前进。 Any ideas? 有任何想法吗?

HTML: HTML:

<html>
  <head>
    <body bgcolor="#00">
    <center><img src="head.png" alt="Head"></center>
  </head>
 <style>
 video {
  position: absolute;
  left: 12vw;
}
.full-frame {
    width:75%%;
    height:75%
}
</style>
<br><br>
<div id="video"; overflow: hidden">
   <video src="1.mov" style="width: 75%; height: 75%;" autoplay loop></video>
    <video src="2.mov" style="width: 75%; height: 75%;" autoplay loop></video>
   <video src="3.mov" style="width: 75%; height: 75%;" autoplay loop></video>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

Try using 尝试使用

document.body.addEventListener() document.body.addEventListener()

instead of document.addEventListener() 代替document.addEventListener()

As I know, keyboard related events are only fires when the element has focus. 据我所知,与键盘相关的事件仅在元素具有焦点时才会触发。 So basically you need give the focus to work correctly. 因此,基本上,您需要重点关注才能正常工作。 What about attaching event on window object? 在窗口对象上附加事件呢?

window.addEventListener('keydown', (e) => console.log(e.keyCode))

I just checked on chrome and FF, both were worked. 我只是检查了chrome和FF,两者都奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery的scrolltotop无法在Chrome浏览器中工作? - jquery scrolltotop not working in browsers except chrome? 画布可以在除Chrome之外的所有浏览器中正常运行 - Canvas Working Fine In All Browsers Except Chrome 输入类型日期,时间在Chrome浏览器以外的其他浏览器上不起作用 - Input type date ,time not working on other browsers except Chrome Cookies功能在除Chrome之外的所有主流浏览器中都能正常运行 - Cookies function working fine in all major browsers except Chrome 请求数据在除Chrome,JavaScript和Contentful API之外的浏览器中不起作用 - Requesting Data not Working in browsers except Chrome, JavaScript and Contentful API window.getComputedStyle不适用于除Chrome之外的其他浏览器的速记属性 - window.getComputedStyle not working for shorthand properties in other browsers except Chrome Keypress在chrome工作但不是firefox - Keypress working in chrome but not firefox 按键无法在Chrome或Firefox中使用 - keypress not working in chrome or firefox Javascript 适用于除 Chrome 以外的所有浏览器 - Javascript works in all browsers except Chrome jQuery滑块可在除Chrome 15之外的所有浏览器上使用。调整Chrome 15浏览器窗口大小时可再次使用 - jQuery slider working on all browsers except Chrome 15. Works again when resizing Chrome 15 browser window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM