1. 程式人生 > 實用技巧 >百度ueditor編輯器

百度ueditor編輯器

官網地址:http://ueditor.baidu.com/website/

下載地址:http://ueditor.baidu.com/website/download.html

我下載了個asp的,目前不確定選擇asp、php、jsp有什麼區別,先下載個asp的看看

下載之後解壓:

看index.html中的程式碼,挺詳細的:

自己敲一遍最好,熟悉一下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content
="width=device-width, initial-scale=1.0"> <script type="text/javascript" charset="utf-8" src="ueditor.config.js"></script> <script type="text/javascript" charset="utf-8" src="ueditor.all.min.js"> </script> <script type="text/javascript" charset="utf-8" src="lang/zh-cn/zh-cn.js"
></script> <title>ueditor編輯器</title> </head> <body> <h1>完整demo</h1> <!-- 編輯器需要一個dom容器 需要有id值 script標籤也可以--> <div id="editor" style="width:100%;height:500px;"></div> <div id="btns"> <div> <
button onclick=" getAllHtml() ">獲得整個html內容</button> <button onclick=" getContent() ">獲得內容</button> <button onclick="setContent('歡迎使用ueditor',false)">寫入內容(替換)</button> <button onclick="setContent('追加內容',true)">追加內容</button> <button onclick="getContentTxt()">獲得純文字</button> <button onclick="getPlainTxt()">獲得帶格式的純文字</button> <button onclick="hasContent()">判斷是否有內容</button> <button onclick="setFocus()">使編輯器獲得焦點</button> <button onmousedown="isFocus(event)">編輯器是否獲得焦點</button> <button onmousedown="setblur(event)" >編輯器失去焦點</button> </div> <div> <button onclick="getText()">獲得當前選中的文字</button> <button onclick="insertHtml()">插入給定的內容</button> <button id="enable" onclick="setEnabled()">可以編輯</button> <button onclick="setDisabled()">不可編輯</button> <button onclick=" UE.getEditor('editor').setHide()">隱藏編輯器</button> <button onclick=" UE.getEditor('editor').setShow()">顯示編輯器</button> <button onclick=" UE.getEditor('editor').setHeight(300)">設定高度為300預設關閉了自動長高</button> </div> <div> <button onclick="getLocalData()" >獲取草稿箱內容</button> <button onclick="clearLocalData()" >清空草稿箱</button> </div> </div> <script type="text/javascript"> //例項化編輯器 //建議使用工廠方法getEditor建立和引用編輯器例項,如果在某個閉包下引用該編輯器,直接呼叫UE.getEditor('editor')就能拿到相關的例項 var ue = UE.getEditor('editor'); function getAllHtml(){ alert(UE.getEditor('editor').getAllHtml()) console.log(UE.getEditor('editor').getAllHtml()); } function getContent(){ let content = UE.getEditor('editor').getContent(); alert(content) } function setContent(content,isAppendTo) { UE.getEditor('editor').setContent('歡迎使用ueditor', isAppendTo); } function getContentTxt() { let txt = UE.getEditor('editor').getContentTxt(); alert(txt); } function getPlainTxt() { let plainTxt = UE.getEditor('editor').getPlainTxt(); alert(plainTxt) console.log(plainTxt); } function hasContent() { // 空格不算 空標籤不算 let hasContent = UE.getEditor('editor').hasContents(); alert(hasContent); } function setFocus() { UE.getEditor('editor').focus(); } 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 getText() { //當你點選按鈕時編輯區域已經失去了焦點,如果直接用getText將不會得到內容,所以要在選回來,然後取得內容 var range = UE.getEditor('editor').selection.getRange(); range.select(); var txt = UE.getEditor('editor').selection.getText(); alert(txt) } function insertHtml() { var value = prompt('插入html程式碼', ''); UE.getEditor('editor').execCommand('insertHtml', value) } function setEnabled() { UE.getEditor('editor').setEnabled(); enableBtn(); } function enableBtn() { var wrap = document.getElementById('btns'); var btns = UE.dom.domUtils.getElementsByTagName(wrap, "button"); for (var i = 0, btn; btn = btns[i++];) { UE.dom.domUtils.removeAttributes(btn, ["disabled"]); } } function setDisabled() { UE.getEditor('editor').setDisabled('fullscreen'); disableBtn("enable"); } function disableBtn(str) { var wrap = document.getElementById('btns'); var btns = UE.dom.domUtils.getElementsByTagName(wrap, "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 getLocalData () { alert(UE.getEditor('editor').execCommand( "getlocaldata" )); } function clearLocalData () { UE.getEditor('editor').execCommand( "clearlocaldata" ); alert("已清空草稿箱") } </script> </body> </html>