0

我刚遇到这个问题。我正在使用空格键来播放/暂停页面上的一个主要播放器。在进行一些测试时,我发现如果我在搜索或评论中输入并按空格键,那么它只会继续播放和暂停播放器,这很烦人。我正在寻找检查输入是否集中的方法,而不是不使用空格键进行播放/暂停,否则将其用于播放暂停,但找不到解决方案。

在逻辑方面,我需要做类似的事情

if (unicode == 32  "SPACEBAR" && "No input is focused, so spacebar is not used for typing ") {

//Some stuff here

 }
4

3 回答 3

2

您可以尝试使用document.activeElement它并检查类型。

例如:

document.activeElement.nodeName === 'INPUT'

所以你的 if 应该是这样的:

if( unicode == '32' && document.activeElement.nodeName !== 'INPUT' ) {
    // play/pause video
}
于 2013-06-09T13:40:06.660 回答
0

检查 document.activeElement

文档

于 2013-06-09T13:39:45.453 回答
0

使用事件目标属性:

$(document).on('keydown', function (e) {
       if(!$(e.target).is('input[type=text]') && unicode === 32) {
           //stuff here
       }
    });
于 2013-06-09T13:41:44.213 回答