Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript event.preventDefault()停止在Firefox中处理产品_Javascript_Cross Browser - Fatal编程技术网

javascript event.preventDefault()停止在Firefox中处理产品

javascript event.preventDefault()停止在Firefox中处理产品,javascript,cross-browser,Javascript,Cross Browser,我有一个javascript方法,它引入了两个键盘快捷键,包括一个覆盖现有浏览器命令的快捷键。Chrome和Firefox中的Ctrl-G通常是指向增量搜索中下一项的命令,通常由Ctrl-F启动。对于我的应用程序,我希望Ctrl-G提示输入文档id,并将用户带到相应的文档 在我的本地主机上,它在Chrome和Firefox中都可以正常工作。将阻止正常的Ctrl-G行为,提示显示为正常 当我将完全相同的代码放在生产服务器上时,Chrome仍然可以正常工作,但Firefox同时执行我的脚本操作和默认

我有一个javascript方法,它引入了两个键盘快捷键,包括一个覆盖现有浏览器命令的快捷键。Chrome和Firefox中的Ctrl-G通常是指向增量搜索中下一项的命令,通常由Ctrl-F启动。对于我的应用程序,我希望Ctrl-G提示输入文档id,并将用户带到相应的文档

在我的本地主机上,它在Chrome和Firefox中都可以正常工作。将阻止正常的Ctrl-G行为,提示显示为正常

当我将完全相同的代码放在生产服务器上时,Chrome仍然可以正常工作,但Firefox同时执行我的脚本操作和默认的Ctrl-G操作。更糟糕的是,焦点/光标最终出现在增量搜索而不是提示对话框中,因此用户不能简单地忽略它并键入值

我发现的所有其他类似问题都归结为未在处理程序中定义事件,这一点我是正确的。其他建议是立即执行event.preventDefault,但我只想在某些情况下防止默认,而不是在其他情况下

$(document).bind('keydown', function(e) {
    // ignore keyboard shortcuts when in an input
    if (isFocusOnInput()) { return; }

    // get which key was pressed (upper case if Shift, lower if not)
    var key;
    if (e.shiftKey) {
        key = String.fromCharCode(e.which);
    } else {
        key = String.fromCharCode(e.which).toLowerCase();
    }

    // the shortcuts themselves.
    if (key == 'h' && typeof hilightMyTasks == 'function') {
        hilightMyTasks();
    } else if (key == 'g' && (e.ctrlKey || e.metaKey)) {
        e.preventDefault();
        id = prompt('Enter task id','');
        if (id) {
            if (Number(id) == id && id % 1 == 0 && id > 0) {
                window.location.href='/index.php?m=tasks&a=view&task_id='+id+'&tab=0'
            } else {
                alert('Invalid task id!');
            }
        }
    }
});
如果相关,isFocusOnInput方法:


我只是尝试在方法中的其他操作之前先执行e.preventDefault,但它在Firefox上仍然执行默认的Ctrl-G操作,因此这似乎不是一个速度/并行执行问题它能阻止两个站点上的所有事件,包括ctrl+g吗?@Halcyon我尝试过它,它几乎阻止了除ctrl-g之外的所有按键。。。我甚至不知道。编辑:其他一些仍然有效,如Ctrl-tab,但Ctrl-F和Ctrl-W没有。我也试着用$window代替$document,同样的结果说实话,我很惊讶浏览器会尊重默认的Chrome浏览器。似乎在过去ctrl+f是无法阻止的。我不知道浏览器供应商是否同意这种行为。要尝试三件事:1调用e.stopPropagation和e.stopImmediatePropagation,2使用addEventListener与true绑定,以在捕获阶段取消事件,3绑定并阻止按键事件。
function isFocusOnInput() {
    var focusType = document.activeElement.type;
    if (focusType == 'text' || focusType == 'textarea' || focusType == 'select-one' ||
            focusType == 'checkbox' || focusType == 'radio' || focusType == 'select-multiple' ||
            focusType == 'password' || focusType == 'hidden' || focusType == 'submit' ||
            focusType == 'reset' || focusType == 'image' || focusType == 'number' ||
            focusType == 'color' || focusType == 'date' || focusType == 'datetime' ||
            focusType == 'datetime-local' || focusType == 'email' || focusType == 'month' ||
            focusType == 'range' || focusType == 'search' || focusType == 'tel' ||
            focusType == 'time' || focusType == 'url' || focusType == 'week' ||
            focusType == 'button') {
        return true;
    }
    return false;
}