javascript - 如何在输入键按下时启动/停止选取框?
问题描述
以下是选取框在鼠标单击时停止并在鼠标单击时开始的代码片段。我需要更改它以检测从键盘按下回车键的事件。这怎么可能?谢谢
<marquee behavior="scroll" direction="left" onmousedown="this.stop();" onmouseup="this.start();">
Go on... click me (and hold the mouse down)!
</marquee>
解决方案
<!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>
推荐阅读
- c# - “foreach”中的代码被跳过 C#
- sql - 如何在 laravel 中对分组后的数据求和?
- loops - Kotlin 在循环中删除对象的问题
- node.js - 为什么node的aes-256-gcm能产生长度不能被16整除的密文?
- redis - 在后台运行 Redis 服务器(而不运行终端)?
- c++ - 如何找到其乘积可以表示为 2 个随机整数的平方差的连续子序列/子数组的数量?
- excel - 为什么我的陈述询问两个变量是否相等不起作用?
- dialogflow-es - 获取 TypeError: conv.parameters in not a function at app.intent with Dialogflow fufillment
- node.js - 当本地没有可用的 Node.js 时使用 Husky 和 Docker
- django-rest-framework - DRF登录api返回E-mail已经存在