admin管理员组文章数量:1356782
First off I'll start off by saying I am new to javascript so hopefully this isn't a plete 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>
Instead google chrome console reports:
"Uncaught TypeError: Cannot call method 'getData' of undefined "
Now when I change
var ckvalue = CKEDITOR.instances.editor.getData();
to
var ckvalue = CKEDITOR.instances.editor1.getData();
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: /
First off I'll start off by saying I am new to javascript so hopefully this isn't a plete 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>
Instead google chrome console reports:
"Uncaught TypeError: Cannot call method 'getData' of undefined "
Now when I change
var ckvalue = CKEDITOR.instances.editor.getData();
to
var ckvalue = CKEDITOR.instances.editor1.getData();
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/s3aDC/6/
Share Improve this question edited Dec 28, 2012 at 1:28 Jesse asked Dec 27, 2012 at 23:29 JesseJesse 431 gold badge1 silver badge5 bronze badges 3- 1 Can you post your code or a proof of concept? Hard to tell with the small amount posted. – Kirk Commented Dec 27, 2012 at 23:32
- Here is proof of concept – Jesse Commented Dec 27, 2012 at 23:49
- Looks like you've added your code above which may help. – Kirk Commented Dec 28, 2012 at 0:09
2 Answers
Reset to default 4editor
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() );
}
}
} );
And you should definitely avoid global variables in your code! ;)
CKEditor gets editor name from:
- id or name attribute of textarea/editable element if editor isn't in "append to" mode: https://github./ckeditor/ckeditor-dev/blob/master/core/editor.js#L71
- function that generates unique editors names https://github./ckeditor/ckeditor-dev/blob/master/core/editor.js#L97
In your case editor is created by appendTo()
method so CKEditor automatically generate name which is editor1
, then editor2
, etc. CKEDITOR.instances
object contains all editor instances under their name, that's why there exists CKEDITOR.instances.editor1
.
You assigned editor instance to global editor
variable. But that's something pletely different than editor name - you can have editor instance assigned to as many variables as you wish.
BTW. You should declare variables with var
statement before using them.
本文标签: javascriptckeditor getData() doesnt seem to work as it shouldStack Overflow
版权声明:本文标题:javascript - ckeditor getData() doesnt seem to work as it should - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743948350a2566827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论