我想要获取RichTextArea中光标的位置或者坐标,但是不知道如何在没有鼠标事件的情况下获取当前光标位置。
比如说,TextArea有一个名为getCursorPos()的方法,但是RichTextArea并没有类似于TextArea的方法。
请问有谁有什么好的想法吗?
提前感谢您的回答...
我想要获取RichTextArea中光标的位置或者坐标,但是不知道如何在没有鼠标事件的情况下获取当前光标位置。
比如说,TextArea有一个名为getCursorPos()的方法,但是RichTextArea并没有类似于TextArea的方法。
请问有谁有什么好的想法吗?
提前感谢您的回答...
RichTextArea.Formatter formatter = richText.getFormatter();
formatter.insertHTML("My text is inserted at the cursor position");
CKEditorTextField textArea;
// and for example in some listener function we could call:
textArea.insertHtml("<b>some html</b>");
textArea.insertText("sample text");
试一下这个,对我有用。基本上你在富文本区域插入一个唯一的文本,然后获取插入文本的索引,最后将其删除。
richText=new RichTextArea();
basicFormatter=richText.getFormatter();
basicFormatter.insertHTML("dummydata");
int cursor=richText.getText().indexOf("dummydata");
basicFormatter.undo();
不知道这是否仍然需要,但我今天一直在尝试做完全相同的事情,却没有找到明确的答案。我找到了这个非GWT解决方案(Get caret (cursor) position in contentEditable area containing HTML content),需要微调一下。希望这能帮助到某些人。
public static native int getCursor(Element elem) /*-{
var node = elem.contentWindow.document.body
var range = elem.contentWindow.getSelection().getRangeAt(0);
var treeWalker = $doc.createTreeWalker(node, NodeFilter.SHOW_TEXT, function(node) {
var nodeRange = $doc.createRange();
nodeRange.selectNodeContents(node);
return nodeRange.compareBoundaryPoints(Range.END_TO_END, range) < 1 ? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
}, false);
var charCount = 0;
while (treeWalker.nextNode()) {
charCount += treeWalker.currentNode.length;
}
if (range.startContainer.nodeType == 3) {
charCount += range.startOffset;
}
return charCount;
}-*/;