ckeditor-获取当前光标位置的上一个字符
如何在ckeditor中获取当前光标位置的上一个字符?例如,假设“|”字符是“Hello | World”文本中的光标位置,那么我想得到“o”字符。鉴于ckeditor-获取当前光标位置的上一个字符,ckeditor,Ckeditor,如何在ckeditor中获取当前光标位置的上一个字符?例如,假设“|”字符是“Hello | World”文本中的光标位置,那么我想得到“o”字符。鉴于编辑器是CKEDITOR的一个。实例,下面应该可以做到这一点: function getPrevChar() { var range = editor.getSelection().getRanges()[ 0 ], startNode = range.startContainer; if ( startNode
编辑器是CKEDITOR的一个。实例
,下面应该可以做到这一点:
function getPrevChar() {
var range = editor.getSelection().getRanges()[ 0 ],
startNode = range.startContainer;
if ( startNode.type == CKEDITOR.NODE_TEXT && range.startOffset )
// Range at the non-zero position of a text node.
return startNode.getText()[ range.startOffset - 1 ];
else {
// Expand the range to the beginning of editable.
range.collapse( true );
range.setStartAt( editor.editable(), CKEDITOR.POSITION_AFTER_START );
// Let's use the walker to find the closes (previous) text node.
var walker = new CKEDITOR.dom.walker( range ),
node;
while ( ( node = walker.previous() ) ) {
// If found, return the last character of the text node.
if ( node.type == CKEDITOR.NODE_TEXT )
return node.getText().slice( -1 );
}
}
// Selection starts at the 0 index of the text node and/or there's no previous text node in contents.
return null;
}
检查一下。试一试:将插入符号放在任何地方,但大部分放在^
之前,看看是否涵盖了棘手的情况。通过某种方式修改此函数,我们可以在当前光标位置之前/之后获得完整的html吗?