[英]Fix cursor position correct with contenteditable div
I've tried to make a simple editor box and want to add the checkbox input onto it. 我试图制作一个简单的编辑器框,并想在其上添加复选框输入。
But when I add the checkbox, I found that the cursor didn't go right: 但是,当我添加复选框时,我发现光标没有正确移动:
the caret is before the checkbox I added, and it can't go right even I hit my right arrow key. 插入符号位于我添加的复选框之前,即使我按了向右箭头键也无法正确插入。 Any suggest to make it more correct? 有什么建议可以使其更正确吗? (I'm using a webbrowser based on webkit, just make it correct is fine. ) (我正在使用基于webkit的Web浏览器,只需使其正确就可以了。)
What I want to implement is when I click the mouse or use keyboard to position the content, the caret should work as normal editor(word, evernote, etc). 我要实现的是当我单击鼠标或使用键盘定位内容时,插入符号应作为普通编辑器(word,evernote等)工作。
jsfiddler: http://jsfiddle.net/cppgohan/84CTE/9/ jsfiddler: http : //jsfiddle.net/cppgohan/84CTE/9/
<button id="add"> AddCheckbox </button>
<button id="add2"> AddCheckbox2 </button>
<div id="editor" contenteditable="true" width="500px" height="500">
1. Make it work <input type="checkbox"><br>
2. Make it done <input type="checkbox" checked>
<div>
$(document).ready(function(){
$("#add").click(function(){
$editor = $("#editor");
$editor.append('<input type="checkbox">');
$editor.focus();
});
...
});
It is a long history bug of chromium. 这是铬的悠久历史。
After more and more (re)search, it is a web browser Bug: chromium bug: Caret moves incorrectly around check boxes and radio buttons in contentEditable div 经过越来越多的(重新)搜索,它是一个网络浏览器错误: chrome错误:插入符在contentEditable div中的复选框和单选按钮周围错误地移动
Using the arrow keys to place the caret immediately after a check box or radio button should result in the caret being rendered in that spot. 使用箭头键将插入符号立即置于复选框或单选按钮之后,应导致在该位置渲染插入符号。
The caret is rendered to the left of and slightly down from the radio button or check box. 插入标记显示在单选按钮或复选框的左侧,并稍稍向下。
Seems chrome has postponed the issuefix chrome似乎已推迟了问题修复
I use a <img>
tag instead of real <input>
tag, use checkbox_check
checkbox_uncheck
class to toggle the image check or uncheck. 我使用的是<img>
标记而不是实际的<input>
标记,请使用checkbox_check
checkbox_uncheck
类切换图像的选中或取消选中状态。
It works fine, I also tried to write the function for setting/getting content from the editor view, and use regex::replace
for transforming between <input>
and <img>
它工作正常,我还尝试编写用于从编辑器视图设置/获取内容的功能,并使用regex::replace
在<input>
和<img>
之间进行转换
I have Added some javascript code and now it's work as expected: 我已经添加了一些JavaScript代码,现在可以正常使用了:
function setEndOfContenteditable(contentEditableElement)
{
var range,selection;
if(document.createRange)
{
range = document.createRange();
range.selectNodeContents(contentEditableElement);
range.collapse(false);
selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
else if(document.selection)
{
range = document.body.createTextRange();
range.moveToElementText(contentEditableElement);
range.collapse(false);
range.select();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.