如何在销毁实例后重新启动ckeditor(4),这样我就不必重新加载页面了。我摧毁它们以清理内联div之前(通过ajax保存内容到mysql中看到我的评论)
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Massive inline editing — CKEditor Sample</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="/ckeditor/ckeditor.js"></script>
<script>
// The "instanceCreated" event is fired for every editor instance created.
CKEDITOR.on('instanceCreated', function(event) {
var editor = event.editor,
element = editor.element;
});
</script>
</head>
<body>
<!-- START CONTAINER DIV -->
<div id="container">
<div id="headerLeft" contenteditable="true">
<!-- START HEADERLEFT DIV -->
<h2 id="sampleTitle">
CKEditor<br> Goes Inline!
</h2>
<h3>
Lorem ipsum dolor sit amet dolor duis blandit vestibulum faucibus a, tortor.
</h3>
</div>
<!-- END headerLeft DIV -->
<div id="headerRight" contenteditable="true">
<!-- START HEADERRIGHT DIV -->
<p>Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies.
</p>
<p>Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, mauris nec malesuada
fames ac.
</p>
</div>
<!-- END headerRight DIV -->
</div>
<!-- END CONTAINER DIV -->
<div style="clear:both;"></div>
<input type="button" id="clickME" value="Save Changes">
<script>
$("#clickME").click(function() {
//START FOR STATEMENT
// Cleaning up the div's that contain contenteditable="true" because ckeditor is changing them form
// <div id="headerRight" contenteditable="true"> TO
// <div id="headerLeft" contenteditable="true" class="cke_editable cke_editable_inline cke_contents_ltr" tabindex="0" spellcheck="false" style="position: relative; " role="textbox" aria-label="Rich Text Editor, headerLeft" title="Rich Text Editor, headerLeft" aria-describedby="cke_53">
// Without this for statement
// IS THERE AN ALTERNATIVE TO PREVENT DESTROYING CKEDITOR INSTANCES????
for (name in CKEDITOR.instances) {
CKEDITOR.instances[name].destroy()
}
// END FOR STATEMENT
// GET CONTENTS FROM CONTAINER DIV
var htmlStr = $('#container').html();
// CALL FUNCTION TO SAVE CONTENT FROM CONTAINER DIV VIA AJAX
addHit(htmlStr);
// HOW TO RE INITIALIZE CKEDITOR HERE SO CKEDITOR WORKS AGAIN?????
});
// START FUNCTION TO SAVE DATA VIA AJAX
function addHit(data1) {
$.ajax({
type: "POST",
url: "/save.php",
data: "var1=" + data1,
success: function(msg) {
alert("Data Saved: " + msg); //Anything you want
}
});
}
// END FUNCTION TO SAVE DATA VIA AJAX
</script>
</body>
</html>
答案 0 :(得分:1)
您应该在数据库中存储编辑器的值,而不是整个#container
元素。瞧! - 无需破坏编辑器,更简单的代码,更好的用户体验。
答案 1 :(得分:0)
您可以克隆数据然后保存。这将为您节省干预CKeditor的痛苦。做这样的事情
var data_to_save = $("<div>", {id: 'headerLeft', contenteditable: 'true'}).append("<original div>").children().clone();
答案 2 :(得分:0)
您可以查看以下小提琴中的实际示例
https://jsfiddle.net/samuelj90/0hx0bnk9/2/
var editorInstance;
document.getElementById('toogleEditor').addEventListener('click',function() {
if (CKEDITOR) {
if (editorInstance) {
editorInstance.destroy();
editorInstance = undefined;
} else {
editorInstance = CKEDITOR.replace('editor1');
editorInstance.setData("MY HTML DATA");
}
}
})