javascript 如何在服务器上保存内联编辑器内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13922002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I save inline editor contents on the server?
提问by Dan Temple
I'm using CKeditor to allow users to inline edit the content on a page once logged in.
我正在使用 CKeditor 允许用户在登录后内联编辑页面上的内容。
I know I can access the data using:
我知道我可以使用以下方法访问数据:
var data = CKEDITOR.instances.editable.getData();
but I don't know how to send the data to a script so I can update the database. It would be cool if the script ran each time someone deselected a contenteditable
element... but I don't know if thats even possible.
但我不知道如何将数据发送到脚本以便更新数据库。如果每次有人取消选择一个contenteditable
元素时脚本都运行,那会很酷……但我不知道这是否可能。
Any tips would be great! :)
任何提示都会很棒!:)
My site is built using php/mysql.
我的网站是使用 php/mysql 构建的。
回答by oleq
Something like this:
像这样的东西:
CKEDITOR.disableAutoInline = true;
CKEDITOR.inline( 'editable', {
on: {
blur: function( event ) {
var data = event.editor.getData();
// Do sth with your data...
}
}
} );
Note that this won't work with other interactions like: user called editor.setData()
or user closed the web page while editing. Contents will be lost in such cases. If I were you, I'd rather periodically check for new data:
请注意,这不适用于其他交互,例如:用户editor.setData()
在编辑时呼叫或用户关闭了网页。在这种情况下,内容将丢失。如果我是你,我宁愿定期检查新数据:
CKEDITOR.disableAutoInline = true;
var editor = CKEDITOR.inline( 'editable', {
on: {
instanceReady: function() {
periodicData();
}
}
} );
var periodicData = ( function(){
var data, oldData;
return function() {
if ( ( data = editor.getData() ) !== oldData ) {
oldData = data;
console.log( data );
// Do sth with your data...
}
setTimeout( periodicData, 1000 );
};
})();
回答by kei.
CKEDITOR.inline('editable' ,{
on:{
blur: function(event){
if (event.editor.checkDirty())
console.log(event.editor.getData());
}
}
});
Try this.
试试这个。
回答by Andriy Kuba
It looks like "blur" event might help you: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#event:blur
看起来“模糊”事件可能对您有所帮助:http: //docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#event:blur