js 在文本字段中输入时停用空格键
我正在创建一个音乐播放器,它使用空格键暂停和播放音频,现在可以知道某人当前是否正在文本字段中输入,因为现在,如果该用户输入文本字段并且正在播放音频,那么当用户按空格键音频暂停,我基本上希望当用户不在文本字段中时,用户可以播放和暂停音频,但是当用户键入时,此功能被禁用,以便音频继续播放。下面的代码是我当前用于检测何时按下空格的代码;
//IF Space bar is Pressed
$(window).keypress(function(e) {
if(e.keyCode == 32) {
if(document.getElementById('audio').paused){
if(document.getElementById('video').style.display=="block"){
}
else{
document.getElementById('audio').play();
document.getElementById('pause').style.display="block";
document.getElementById('play').style.display="none";
}
}
else{
if(document.getElementById('video').style.display=="block"){
}
else{
document.getElementById('audio').pause();
document.getElementById('pause').style.display="none";
document.getElementById('play').style.display="block";
}
}
}
});
//END IF Space bar is pressed
I am creating a music player which uses the spacebar to pause and play audio, now is it possible to know if someone is currently typing in a textfield, because right now, if this user types in a textfield and audio is playing, when the user presses space the audio pauses, I would like basically that when the user is not in a textfield that a user can just play and pause audio, but when the user is typing that this function is disabled so the audio keeps playing. The code below, is the code I'm am current using for detecting when space is pressed;
//IF Space bar is Pressed
$(window).keypress(function(e) {
if(e.keyCode == 32) {
if(document.getElementById('audio').paused){
if(document.getElementById('video').style.display=="block"){
}
else{
document.getElementById('audio').play();
document.getElementById('pause').style.display="block";
document.getElementById('play').style.display="none";
}
}
else{
if(document.getElementById('video').style.display=="block"){
}
else{
document.getElementById('audio').pause();
document.getElementById('pause').style.display="none";
document.getElementById('play').style.display="block";
}
}
}
});
//END IF Space bar is pressed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查事件源
tagName
属性:Check the event sources
tagName
attribute:您也可以尝试
新代码
You could also try
New code
检查文本区域是否具有焦点
逻辑,如果您愿意使用 jQuery,则可能是这样的:
Check if a textarea has focus
Logic for that could be something like this, if you're willing to use jQuery: