[英]How to move the carret in a contenteditable div using jQuery?
I am trying to position the caret in a contenteditable div. 我正在尝试将插入符号放置在一个内容可编辑的div中。 The div does not have setSelectionRange or createTextRange. div没有setSelectionRange或createTextRange。 (I am using Chrome at the moment.) (我目前正在使用Chrome。)
So I tested if I could send keypress events instead: 所以我测试了是否可以发送按键事件:
var e = jQuery.Event("keypress", { keyCode : 39} );
jQuery(myDiv).trigger(e);
It does not work. 这是行不通的。 The event does not show up in the handler on myDiv. 该事件不会显示在myDiv的处理程序中。 <= Wrong. <=错误。 It does show up in the listener, but the caret is not moved. 它确实显示在侦听器中,但插入符未移动。 (keyCode=39 is right arrow.) (keyCode = 39是右箭头。)
Any suggestions? 有什么建议么?
The event should show up when you bind the handler before you trigger it: 绑定处理程序之前,应触发该事件:
(function($){
$(function() {
$('#foobar').keypress(function(event) {
console.log(event.which);
});
var e = $.Event("keypress", { which : 39} );
$('#foobar').trigger(e);
});
})(jQuery);
this logs 39
in the console right when you visit the page. 当您访问该页面时, 39
在控制台中记录39
。
Due to the edit of the question, which is totally different to what you first asked, check this out: 由于问题的编辑与您最初提出的完全不同,请检查以下内容:
How to move cursor to end of contenteditable entity 如何将光标移动到内容可编辑实体的末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.