我的目标是在文本框内容的开头插入html代码,而不考虑插入符号的位置。我修改了下面的脚本(在这里找到),但它不工作,所以我需要知道什么可能是错误的。
CKEDITOR.plugins.add( 'htmlbuttons',
{ init : function( editor )
{ for (name in CKEDITOR.instances) {
var instance = CKEDITOR.instances[name]; }上面我获得了id (与名称相同)-验证
function setSelectionRange(input, selectionStart, selectionEnd) {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToPos (input, pos) {
setSelectionRange(input, pos, pos);
}上面是我在这里找到的重新定位插入符号(游标)的脚本。
var buttonsConfig = editor.config.htmlbuttons;
if (!buttonsConfig)
return;
function createCommand( definition )
{
return {
exec: function( editor ) {
instanceId = (instance.name);
setCaretToPos(document.getElementById(instanceId),0);上面的行应该将插入符号定位在ckeditor文本框的开头,但它不起作用。
editor.insertHtml( definition.html );
}
};
}
// Create the command for each button
for(var i=0; i<buttonsConfig.length; i++)
{
var button = buttonsConfig[ i ];
var commandName = button.name;
editor.addCommand( commandName, createCommand(button, editor) );
editor.ui.addButton( commandName,
{
label : button.title,
command : commandName,
icon : this.path + button.icon
});
}
} //Init
} );发布于 2016-10-26 19:56:26
这是一个更好的解决办法:
换行“SetCaretToPos.”使用此代码:
(function( cursorManager ) {
var range = editor.createRange();
range.moveToPosition( range.root, CKEDITOR.POSITION_BEFORE_END );
//OR range.moveToPosition( range.root, CKEDITOR.POSITION_AFTER_START );
editor.getSelection().selectRanges( [ range ] );
}( window.cursorManager = window.cursorManager || {}));发布于 2016-10-10 16:42:46
我找到了答案--把“SetCaretToPos.”使用此代码:
editor.focus();
var selection = editor.getSelection();
var range = selection.getRanges()[0];
var pCon = range.startContainer.getAscendant('p',true);
var newRange = new CKEDITOR.dom.range(range.document);
newRange.moveToPosition(pCon, CKEDITOR.POSITION_BEFORE_START);
newRange.select();就这样。它在开始时插入代码--不考虑光标的位置。
https://stackoverflow.com/questions/39961949
复制相似问题