1. 程式人生 > >兩款富文字編輯器——Kindeditor+UEditor

兩款富文字編輯器——Kindeditor+UEditor

菜菜君最近第一次使用富文字編輯器,折騰了半天才摸到了一點門道。先概括一下。kindeditor比UEditor的功能少,但是也算五臟俱全,基本的功能都有了。另外,UEditor的介面感覺更好看。但是,菜菜君覺得,UEditor用起來更復雜啊啊啊啊!至少,菜菜君現在還沒弄好...希望高人路過指點一二。

一、富文字編輯器kindeditor


網址:http://kindeditor.net/demo.php


下載KindEditor


引入kindeditor-min.js、zh_CN.js、default.css


將plugins資料夾放在js資料夾下面


<textarea name="content" style="width:800px;height:400px;visibility:hidden;">你好!</textarea>




<script type="text/javascript">


var editor;


KindEditor.ready(function(K) {
editor = K.create('textarea[name="content"]', {
allowFileManager : true
});
K('input[name=getHtml]').click(function(e) {
alert(editor.html());
});
K('input[name=isEmpty]').click(function(e) {
alert(editor.isEmpty());
});
K('input[name=getText]').click(function(e) {
alert(editor.text());
});
K('input[name=selectedHtml]').click(function(e) {
alert(editor.selectedHtml());
});
K('input[name=setHtml]').click(function(e) {
editor.html('<h3>Hello KindEditor</h3>');
});
K('input[name=setText]').click(function(e) {
editor.text('<h3>Hello KindEditor</h3>');
});
K('input[name=insertHtml]').click(function(e) {
editor.insertHtml('<strong>插入HTML</strong>');
});
K('input[name=appendHtml]').click(function(e) {
editor.appendHtml('<strong>新增HTML</strong>');
});
K('input[name=clear]').click(function(e) {
editor.html('');
});
});

</script>



文字域大小固定:


在kindeditor.js中243行,resizeType 2或1或0,2時可以拖動改變寬度和高度,1時只能改變高度,0時
不能拖動。




二、富文字編輯器UEditor


網址:http://ueditor.baidu.com/website/onlinedemo.html


(1)下載UEditor Development


在頁面目錄下包含進third-party、lang、dialogs、themes四個資料夾


<script id="editor" type="text/plain" style="width:750px;height:300px;">你好!</script>


引入ueditor.config.js、ueditor.all.js、zh-cn.js


var ue = UE.getEditor('editor');


    function isFocus(e){
        alert(UE.getEditor('editor').isFocus());
        UE.dom.domUtils.preventDefault(e)
    }
    function setblur(e){
        UE.getEditor('editor').blur();
        UE.dom.domUtils.preventDefault(e)
    }
    function insertHtml() {
        var value = prompt('插入html程式碼', '');
        UE.getEditor('editor').execCommand('insertHtml', value)
    }
    function createEditor() {
        enableBtn();
        UE.getEditor('editor');
    }
    function getAllHtml() {
        alert(UE.getEditor('editor').getAllHtml())
    }
    function getContent() {
        var arr = [];
        arr.push("使用editor.getContent()方法可以獲得編輯器的內容");
        arr.push("內容為:");
        arr.push(UE.getEditor('editor').getContent());
        alert(arr.join("\n"));
    }
    function getPlainTxt() {
        var arr = [];
        arr.push("使用editor.getPlainTxt()方法可以獲得編輯器的帶格式的純文字內容");
        arr.push("內容為:");
        arr.push(UE.getEditor('editor').getPlainTxt());
        alert(arr.join('\n'))
    }
    function setContent(isAppendTo) {
        var arr = [];
        arr.push("使用editor.setContent('歡迎使用ueditor')方法可以設定編輯器的內容");
        UE.getEditor('editor').setContent('歡迎使用ueditor', isAppendTo);
        alert(arr.join("\n"));
    }
    function setDisabled() {
        UE.getEditor('editor').setDisabled('fullscreen');
        disableBtn("enable");
    }


    function setEnabled() {
        UE.getEditor('editor').setEnabled();
        enableBtn();
    }


    function getText() {
        //當你點選按鈕時編輯區域已經失去了焦點,如果直接用getText將不會得到內容,所以要在選回來,然後取得內容
        var range = UE.getEditor('editor').selection.getRange();
        range.select();
        var txt = UE.getEditor('editor').selection.getText();
        alert(txt)
    }


    function getContentTxt() {
        var arr = [];
        arr.push("使用editor.getContentTxt()方法可以獲得編輯器的純文字內容");
        arr.push("編輯器的純文字內容為:");
        arr.push(UE.getEditor('editor').getContentTxt());
        alert(arr.join("\n"));
    }
    function hasContent() {
        var arr = [];
        arr.push("使用editor.hasContents()方法判斷編輯器裡是否有內容");
        arr.push("判斷結果為:");
        arr.push(UE.getEditor('editor').hasContents());
        alert(arr.join("\n"));
    }
    function setFocus() {
        UE.getEditor('editor').focus();
    }
    function deleteEditor() {
        disableBtn();
        UE.getEditor('editor').destroy();
    }
    function disableBtn(str) {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            if (btn.id == str) {
                UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
            } else {
                btn.setAttribute("disabled", "true");
            }
        }
    }
    function enableBtn() {
        var div = document.getElementById('btns');
        var btns = UE.dom.domUtils.getElementsByTagName(div, "button");
        for (var i = 0, btn; btn = btns[i++];) {
            UE.dom.domUtils.removeAttributes(btn, ["disabled"]);
        }
    }


    function getLocalData () {
        alert(UE.getEditor('editor').execCommand( "getlocaldata" ));
    }


    function clearLocalData () {
        UE.getEditor('editor').execCommand( "clearlocaldata" );
        alert("已清空草稿箱")
    }


在ueditor.config.js中保留需要的功能按鈕


求助:各位大大們,你們知道為什麼菜菜君所有的對話方塊窗體內容都顯示不出來嗎!?它們每次出來都是醬的!




(2)下載UEditor Builder

沒下載成功吶,改天再試試!