Estoy tratando de agregar un fragmento de texto a un CKEditor existente usando jQuery. Esto debe hacerse cuando se hace clic en un enlace.Insertar texto en la posición del cursor a un CKEditor usando jQuery
he intentado esta solución, que trabaja para áreas de texto normales, pero no para CKEditor:
jQuery.fn.extend({
insertAtCaret: function(myValue) {
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
} else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
})
}
});
También hay una opción para utilizar: $('#editor').val()
, pero esto añade el texto al final o al principio y no en el cursor.
Entonces, ¿hay alguna manera de lograr esto?
Gran! ¡Gracias! Hice un ligero cambio: 'CKEDITOR.instances [$ (this) .attr (" id ")]. InsertText (myValue);' así que no tengo que preocuparme por la identificación. – Phoenix
aplausos !!! @Fénix – Devjosh