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检查焦点是否在可编辑字段上,如果有效,请在此处发布答案:)在我的案例中没有帮助。页面仍然跳转