不知道這是BUG還是UE本身的產品設計,但是真的很容易讓人造成困擾,所以還是決定改一下,說是改其實也等於取巧在JS中做了一下處理,方便,又不涉及源碼。
修改分為兩部分:
1)把當前的編輯器form表單提交修改為Javascript方式提交。
<form action="index.php" method="POST" name="myForm">
form表單加入name元素。
<button class="btn2">提交</button>
submit提交改為button方式。
<script type="text/javascript">
function submitForm(){
document.myForm.action = document.myForm.action;
document.myForm.submit();
}
$(".btn2").click(function(){
submitForm();
})
</script>
加入js表單提交事件。
2)通過UEditor API中的editor.execCommand( 'source')方法事件,在原始碼狀態提交時切換為編輯模式。
<script type="text/javascript">
var ue = UE.getEditor('editor',{
toolbars: [["undo","redo","|","bold","italic","underline","strikethrough","|","fontsize","forecolor","backcolor","|","removeformat","|","selectall","cleardoc","source","|","unlink","link","|","insertimage"]],wordCount:false
});
function submitForm(){
document.myForm.action = document.myForm.action;
document.myForm.submit();
}
$(".btn2").click(function(){
ue.execCommand('source');
submitForm();
})
</script>
var ue = UE.getEditor為UE執行個體化對象,在btn2點擊事件中加入execCommand('source')方法,此方法在submitForm前執行,提交後成功儲存所編輯內容。
此次修改不涉及服務端代碼,服務端代碼可保持原樣,依舊post接收表單資訊內容。
在使用UEditor API時如出現下面JS載入錯誤資訊:
Uncaught typeerror cannot read property 'xxx' of undefined。
解決方式如下:
1)ueditor.config.js和ueditor.all.min.js載入順序所致,載入循序要查看目前的版本文檔。
2)多次執行個體化或者未定義editor對象,UE.getEditor和new UE.ui.Editor()使用一個即可