上一篇《初試JqueryEasyUI(附Demo)》;javascript
在上一篇說過,下面要試下easyui集合編輯器,關於編輯器網上有不少,ckeditor、ueditor、kindeditor、eWebEditor等,其實最先接觸的是ckeditor(fckeditor),用着功能確實不錯,可是感受太複雜了,並且東西也比較大,不是很方便,ueditor是百度出品的,可是用過kindeditor以後發現感受仍是kindeditor比較好用點,我的感受,勿噴!html
kindeditor的示例也是比較全的,並且兼容性也比較好,就用它試着集合easyui了。java
因本人js技術有限,試了很久也沒搞好,此處略去十萬個字。。。編輯器
網上找到一段js代碼:post
1 (function ($, K) { 2 if (!K) 3 throw "KindEditor未定義!"; 4 5 function create(target) { 6 var opts = $.data(target, 'kindeditor').options; 7 var editor = K.create(target, opts); 8 $.data(target, 'kindeditor').options.editor = editor; 9 } 10 11 $.fn.kindeditor = function (options, param) { 12 if (typeof options == 'string') { 13 var method = $.fn.kindeditor.methods[options]; 14 if (method) { 15 return method(this, param); 16 } 17 } 18 options = options || {}; 19 return this.each(function () { 20 var state = $.data(this, 'kindeditor'); 21 if (state) { 22 $.extend(state.options, options); 23 } else { 24 state = $.data(this, 'kindeditor', { 25 options : $.extend({}, $.fn.kindeditor.defaults, $.fn.kindeditor.parseOptions(this), options) 26 }); 27 } 28 create(this); 29 }); 30 } 31 32 $.fn.kindeditor.parseOptions = function (target) { 33 return $.extend({}, $.parser.parseOptions(target, [])); 34 }; 35 36 $.fn.kindeditor.methods = { 37 editor : function (jq) { 38 return $.data(jq[0], 'kindeditor').options.editor; 39 } 40 }; 41 42 $.fn.kindeditor.defaults = { 43 resizeType : 1, 44 allowPreviewEmoticons : false, 45 allowImageUpload : false, 46 items : [ 47 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 48 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 49 'insertunorderedlist', '|', 'emoticons', 'image', 'link'], 50 afterChange:function(){ 51 this.sync(); 52 } 53 }; 54 $.parser.plugins.push("kindeditor"); 55 })(jQuery, KindEditor);
須要同時引用easyui和kindeditor相關樣式和腳本,而後就能夠像使用easyui組件同樣使用kindeditor:ui
1 <textarea name="easyui_ditor" id="easyui_ditor" class="easyui-kindeditor" style="width: 100%; height: 200px; visibility: hidden;">EasyUI集合KindEditor</textarea>
若是你使用的是後臺獲取設置kindeditor值的話可使用這個,可是js獲取或設置文本框值,上面就很差實現,也試了不少方法沒有解決,有關js的大神若是知道方法還請賜教。this
注意建立kindeditor的時候有個afterChange事件,表示更改編輯器的內容發生的事件,這邊須要重寫下。其實kindeditor不集合到easyui中也是可使用,只不過沒有上面這樣建立方便,作了個示例,你們能夠看下:spa
1 <tr> 2 <td>easyui-kindeditor編輯器:</td> 3 <td> 4 <textarea name="easyui_ditor" id="easyui_ditor" class="easyui-kindeditor" style="width: 100%; height: 200px; visibility: hidden;">EasyUI集合KindEditor</textarea> 5 <a href="javascript:void(0)" class="easyui-linkbutton" onclick="">設置</a> 6 <a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(KindEditor.html())">獲取</a> 7 </td> 8 </tr> 9 <tr> 10 <td>kindeditor編輯器:</td> 11 <td> 12 <textarea name="txtContent" id="txtContent" style="width: 100%; height: 200px; visibility: hidden;">KindEditor</textarea> 13 <a href="javascript:void(0)" class="easyui-linkbutton" onclick="editor.html('我在設置KindEditor內容');">設置</a> 14 <a href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(editor.html())">獲取</a> 15 </td> 16 </tr>
js代碼:code
1 //編輯器 2 var editor; 3 KindEditor.ready(function (K) { 4 editor = K.create('textarea[name="txtContent"]', { 5 allowFileManager: true, 6 resizeType: 1, 7 allowPreviewEmoticons: false, 8 items: [ 9 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 10 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 11 'insertunorderedlist', '|', 'emoticons', 'image', 'link'] 12 }); 13 });
效果:orm
完整示例Demo下載:http://pan.baidu.com/s/1c0zP6KC