Javascript 如何在按enter键时启动/停止选框?

Javascript 如何在按enter键时启动/停止选框?,javascript,jquery,html,css,html5-canvas,Javascript,Jquery,Html,Css,Html5 Canvas,下面是marquee的代码片段,它可以在鼠标单击“向下”时停止,在鼠标单击“向上”时开始。我需要更改它以检测从键盘按enter键的事件。这怎么可能?谢谢 <marquee behavior="scroll" direction="left" onmousedown="this.stop();" onmouseup="this.start();"> Go on... click me (and hold the mouse down)! </marquee> 继续。。。

下面是marquee的代码片段,它可以在鼠标单击“向下”时停止,在鼠标单击“向上”时开始。我需要更改它以检测从键盘按enter键的事件。这怎么可能?谢谢

<marquee behavior="scroll" direction="left" onmousedown="this.stop();" onmouseup="this.start();">
Go on... click me (and hold the mouse down)!
</marquee>

继续。。。点击我(并按住鼠标)!

继续。。。点击我(并按住鼠标)!
if(document.readyState){
const marquee=document.getElementById('marquee');
document.body.addEventListener('keydown',函数(事件){
如果(event.key=='Enter'){
停下来
}
})
document.body.addEventListener('keyup',函数(事件){
如果(event.key=='Enter'){
marquee.start()
}
})
}
看看这篇Geeksforgeks的文章,这篇文章应该会有所帮助,只是作为一个注释:
 <!DOCTYPE html>
 <html>
      <body>
         <marquee id="marquee" behavior="scroll" direction="left" 
          onmousedown="this.stop();" onmouseup="this.start();">
          Go on... click me (and hold the mouse down)!
          </marquee>
          <script>
                 if(document.readyState){
                     const marquee = document.getElementById('marquee');
                     document.body.addEventListener('keydown',function(event){
                     if(event.key == 'Enter'){
                          marquee.stop()
                     }
                 })
                     document.body.addEventListener('keyup',function(event){
                         if(event.key == 'Enter'){
                     marquee.start()
        }
    })
}

</script>
</body>
</html>