js 在文本字段中输入时停用空格键

发布于 2024-11-15 06:21:14 字数 971 浏览 2 评论 0原文

我正在创建一个音乐播放器,它使用空格键暂停和播放音频,现在可以知道某人当前是否正在文本字段中输入,因为现在,如果该用户输入文本字段并且正在播放音频,那么当用户按空格键音频暂停,我基本上希望当用户不在文本字段中时,用户可以播放和暂停音频,但是当用户键入时,此功能被禁用,以便音频继续播放。下面的代码是我当前用于检测何时按下空格的代码;

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

圈圈圆圆圈圈 2024-11-22 06:21:14

检查事件源 tagName 属性:

function(e) {
 e = e || event;
 var el = e.srcElement || e.target,
     cando = !(/textarea|input/i.test(el.tagName));

 if(cando && e.keyCode == 32)  {
   /etc.
 }
}

Check the event sources tagName attribute:

function(e) {
 e = e || event;
 var el = e.srcElement || e.target,
     cando = !(/textarea|input/i.test(el.tagName));

 if(cando && e.keyCode == 32)  {
   /etc.
 }
}
掩耳倾听 2024-11-22 06:21:14

您也可以尝试

if(e.keyCode == 32 && document.activeElement != document.getElementById('someTextBox'))

新代码

//IF Space bar is Pressed
$(window).keypress(function(e) {
if(e.keyCode == 32) {
    var inputs = document.getElementsByTagName('input');

    for(var item in inputs)
    {
        if(inputs[item] == document.activeElement)
            return;
    }

    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

You could also try

if(e.keyCode == 32 && document.activeElement != document.getElementById('someTextBox'))

New code

//IF Space bar is Pressed
$(window).keypress(function(e) {
if(e.keyCode == 32) {
    var inputs = document.getElementsByTagName('input');

    for(var item in inputs)
    {
        if(inputs[item] == document.activeElement)
            return;
    }

    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
只涨不跌 2024-11-22 06:21:14

检查文本区域是否具有焦点

if(e.keyCode == 32 && !text_area_has_focus) { ... 

逻辑,如果您愿意使用 jQuery,则可能是这样的:

if(e.keyCode == 32 && !$('input[type=text]:focus').length) { ... 

Check if a textarea has focus

if(e.keyCode == 32 && !text_area_has_focus) { ... 

Logic for that could be something like this, if you're willing to use jQuery:

if(e.keyCode == 32 && !$('input[type=text]:focus').length) { ... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文