问题描述
首先,我首先要说我是javascript的新手,所以希望这不是一个完整的问题。话虽这么说,以下代码应该在用户点击它时提醒编辑器的值。
First off I'll start off by saying I am new to javascript so hopefully this isn't a complete face palm question. That being said, the following code should alert the value of the editor when the user clicks off of it.
<script type='text/javascript'>
function openEditor(){
html = "Hello World";
config = {
startupFocus : true
};
editor = CKEDITOR.appendTo( 'textBox', config, html );
if (editor) {
editor.on('blur', function(event) {
var ckvalue = CKEDITOR.instances.editor.getData();
alert(ckvalue);
});
}
}
</script>
<html>
<a href='#' onclick='openEditor()'>Open Editor</a><br />
<div id='textBox'></div>
</html>
相反谷歌Chrome控制台报告:
Instead google chrome console reports:
未捕获的TypeError:无法调用未定义的方法'getData'
现在我改变了
var ckvalue = CKEDITOR.instances.editor.getData();
到
var ckvalue = CKEDITOR.instances.editor1.getData();
它有效。这让我感到困惑,因为我从未声明过editor1实例。我希望有更多经验的人可以向我解释为什么当编辑器没有时,editor1会起作用。
It works. This baffles me because I never declared a editor1 instance. I was hoping someone with a little more experience could explain to me why editor1 works when editor doesnt.
以下是我所说的一个工作示例:
Here is a working example of what Im talking about: http://jsfiddle.net/s3aDC/6/
推荐答案
编辑器
是一个JS变量,指向 CKEDITOR.instances.editor1
。看到编辑器=== CKEDITOR.instances.editor1 // true
。
editor
is a JS variable that points to CKEDITOR.instances.editor1
. See that editor === CKEDITOR.instances.editor1 // true
.
此外,事件回调在编辑
上下文,所以此
指向编辑
:
Moreover, event callback is executed in editor
context, so this
points to editor
:
editor.on('blur', function(event) {
var ckvalue = this.getData();
alert(ckvalue);
});
您可以在初始化编辑器时定义它:
And you can define it when initializing the editor:
var editor = CKEDITOR.appendTo( 'textBox', {
on: {
blur: function( event ) {
console.log( this.getData() );
}
}
} );
你绝对应该! ;)
这篇关于ckeditor getData()似乎没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!