简体   繁体   English

在contenteditable div中使用箭头键移动插入符号时,停止不必要的滚动

[英]Stop unwanted scrolling when moving caret with arrow keys in contenteditable div

Keydown works perfectly when I select document , * , body or any particular element. 当我选择document*body或任何特定元素时,Keydown可以完美地工作。

But when I add .not('.some-class') , keydown works like this .not() doesn't even exist. 但是,当我添加.not('.some-class') ,keydown这样的工作原理.not()甚至不存在。 Maybe because of the way keydown affects children elements, but I'm not sure: 也许是因为keydown影响子元素的方式,但是我不确定:

$('*').not('.some-class').on('keydown',function(e){ 
    var key = e.charCode || e.keyCode;
    if(key == 33 || key == 34 || key == 35 || key == 36 || key == 37 || key == 38 || key == 39 || key == 40 ) {
        e.preventDefault();
    } else {}
});

How to disable these keys for the whole document except for 1 child class? 如何禁用除1个子类以外的整个文档的这些键?

edit: http://jsfiddle.net/umL139xw/2/ 编辑: http //jsfiddle.net/umL139xw/2/

How to stop this unwanted scrolling while keeping ability to move caret with arrows? 如何在保持箭头移动插入符号的同时停止这种不必要的滚动?

edit2: complete solution thanks to Jason P and Kaiido edit2:完整的解决方案,感谢Jason P和Kaiido

http://jsfiddle.net/umL139xw/5/ http://jsfiddle.net/umL139xw/5/

Events bubble (well, a good number of them do). 事件冒泡(嗯,其中很多确实如此)。 That means they fire on the target of the event, then on every element up the DOM tree, so even if you don't bind the handler to .some-class , it will fire for that element's ancestors. 这意味着它们会在事件的目标上触发,然后在DOM树上的每个元素上触发,因此即使您不将处理程序绑定到.some-class ,它也会为该元素的祖先触发。 Also, binding an event handler to * is generally not a good idea. 另外,将事件处理程序绑定到*通常不是一个好主意。 Maybe something like this would work for you? 也许这样的事情适合您?

http://jsfiddle.net/j3wqpdow/ http://jsfiddle.net/j3wqpdow/

$(document).on('keydown',function(e){ 
    console.log(this, e.target);
});

$('.some-class').on('keydown', function(e) {
   e.stopPropagation(); 
});

You could use the cursor's position detector from this answer and then preventDefault() only when you're at the end. 您可以从此答案中使用光标的位置检测器,然后仅在最后时才使用preventDefault()

$(document).on('keydown',function(e){
    console.log(this, e.target);
    var key = e.charCode || e.keyCode;
    if(key == 16 || key == 32 || key == 33 || key == 34 || key == 35 || key == 36 || key == 37 || key == 38 || key == 39 || key == 40 ) {
        e.preventDefault();
    } else {}
});
$('.b').on('keydown', function(e) {
  e.stopPropagation(); 
  var key = e.charCode || e.keyCode;
   //Above part comes from https://stackoverflow.com/questions/7451468/contenteditable-div-how-can-i-determine-if-the-cursor-is-at-the-start-or-end-o/7478420#7478420
    range = window.getSelection().getRangeAt(0)
    post_range = document.createRange();
    post_range.selectNodeContents(this);
    post_range.setStart(range.endContainer, range.endOffset);
    next_text = post_range.cloneContents();

    if( next_text.textContent.length === 0 && key == 39 ){
        e.preventDefault();
    }
});

Working fiddle 工作提琴

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

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