Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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 在不影响文本输入的情况下,防止空格键向下滚动的默认设置_Javascript_Jquery_Events_Preventdefault - Fatal编程技术网

Javascript 在不影响文本输入的情况下,防止空格键向下滚动的默认设置

Javascript 在不影响文本输入的情况下,防止空格键向下滚动的默认设置,javascript,jquery,events,preventdefault,Javascript,Jquery,Events,Preventdefault,我不希望空格键导致页面滚动。 但我希望在输入或内容可编辑分区中按预期工作 我尝试过类似的方法,但页面仍然向下滚动 $(document).on('keydown', function (e) { var $target = $(e.originalEvent.target); if (!$target.closest('[contenteditable="true"]') && !$target.closest('input')){

我不希望空格键导致页面滚动。 但我希望在输入或内容可编辑分区中按预期工作

我尝试过类似的方法,但页面仍然向下滚动

$(document).on('keydown', function (e) {
        var $target = $(e.originalEvent.target);
        if (!$target.closest('[contenteditable="true"]') && !$target.closest('input')){
            e.preventDefault();
        }
    });

下面是获取聚焦元素、检查输入或contenteditable以及空格键的示例。如果您的条件不满足,则阻止默认操作

$(document).on("keypress", function(e) {
    var $focusElem = $(":focus");
    if(e.which == 32 && !($focusElem.is("input") || $focusElem.attr("contenteditable") == "true"))
        e.preventDefault();
});

我怀疑您绑定到每个可编辑字段的“onfocus”事件。如果没有一个文本字段有焦点,那么
preventDefault
to your heart content:)好主意。我将尝试使用document.activeElement检查焦点是否在可编辑字段上,如果有效,请在此处发布答案:)在我的案例中没有帮助。页面仍然跳转