简体   繁体   English

如果焦点位于文本字段和输入上,则启用/禁用击键

[英]enable/disable keystrokes if focus is on textfield and input

I am using onepagescroll on a simple portfolio page. 我在一个简单的投资组合页面上使用onepagescroll I wanted to add spacebar functionality and found the code in the plug-in to modify. 我想添加空格键功能,并在插件中找到要修改的代码。

Now it scrolls with the spacebar, however after testing a contact form on the page I notice that none of the keys associated with the plug-in work while focus is on a textarea or input. 现在它使用空格键滚动,但是在页面上测试联系表单之后,我注意到,当焦点位于textarea或输入时,与插件相关联的任何键都不起作用。

If I remove a case for a key, the textarea works as expected and I can move up/down/indent with space, so the problem is somewhere inside this code: 如果我删除了一个键的情况,textarea按预期工作,我可以向上/向下/缩进空格,所以问题出在这段代码中:

    _keydownHandler = function(e) {
    var tag = e.target.tagName.toLowerCase();

    if (!_hasClass(body, "disabled-onepage-scroll")) {
        switch(e.which) {
            case 38:
                if (tag != 'input' && tag != 'textarea') moveUp(el)
                break;
            case 40:
                if (tag != 'input' && tag != 'textarea') moveDown(el)
                break;
            case 32:
                if (tag != 'input' && tag != 'textarea') moveDown(el)
                break;
            default: return;
        }
    }
    return false;
}

if(settings.keyboard == true) {
    document.onkeydown = _keydownHandler;
}
return false;
}

I have been trying to wrap my head around it but my knowledge of js is limited, the code seems to make sense as is. 我一直试图绕过它,但我对js的了解是有限的,代码似乎是有道理的。 Is there an error somewhere or do I need to write an else statement? 某处有错误还是我需要写一个else语句?

just figured it out, will post the answer in case this comes up for anyone else. 只是想出来,将发布答案,以防其他人出现。

if (!_hasClass(body, "disabled-onepage-scroll")) {
    switch(e.which) {
        case 38:
            if (tag != 'input' && tag != 'textarea') moveUp(el)
            break;
        case 40:
            if (tag != 'input' && tag != 'textarea') moveDown(el)
            break;
        case 32:
            if (tag != 'input' && tag != 'textarea') moveDown(el)
            break;
        default: return;
    } 
return true;  //add this
}
return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM