首页 > 解决方案 > 如何在输入键按下时启动/停止选取框?

问题描述

以下是选取框在鼠标单击时停止并在鼠标单击时开始的代码片段。我需要更改它以检测从键盘按下回车键的事件。这怎么可能?谢谢

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

标签: javascriptjqueryhtmlcsshtml5-canvas

解决方案


 <!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> 

推荐阅读