
文章插圖
wangEditor —— 輕量級(jí) web 富文本編輯器 , 配置方便 , 使用簡(jiǎn)單 。
事例
基本使用NPMnpm i wangeditor --save安裝后幾行代碼即可創(chuàng)建一個(gè)編輯器:
<div id="div1"><p>歡迎使用 <b>wangEditor</b> 富文本編輯器</p></div>import E from "wangeditor"const editor = new E("#div1")editor.create()CDN<scripttype="text/javascript"src="https://www.520longzhigu.com/uploads/allimg/220429/2101503300-1.jpg"></script><script type="text/javascript">const E = window.wangEditorconst editor = new E("#div1")// 或者 const editor = new E(document.getElementById('div1'))editor.create()</script>設(shè)置編輯區(qū)域的高度編輯區(qū)域高度默認(rèn)為 300px , 可通過以下方式修改 。
const editor = new E('#div1')// 設(shè)置編輯區(qū)域高度為 500pxeditor.config.height = 500// 注意 , 先配置 height , 再執(zhí)行 create()editor.create()菜單和編輯區(qū)域分離如果你想要像 知乎專欄、簡(jiǎn)書、石墨、網(wǎng)易云筆記 這些編輯頁面一樣 , 將編輯區(qū)域和菜單分離 , 也可以實(shí)現(xiàn) 。
這樣 , 菜單和編輯器區(qū)域就是使用者可自己控制的元素 , 可自定義樣式 。例如:將菜單fixed、編輯器區(qū)域高度自動(dòng)增加等 。
<head><style>.toolbar {border: 1px solid #ccc;}.text {border: 1px solid #ccc;min-height: 400px;}</style></head><body><p>container 和 toolbar 分開</p><div><div id="toolbar-container" class="toolbar"></div><p>------ 我是分割線 ------</p><div id="text-container" class="text"></div></div><!-- 引入 wangEditor.min.js --><script>const E = window.wangEditorconst editor = new E('#toolbar-container', '#text-container') // 傳入兩個(gè)元素editor.create()</script></body>從上面代碼可以看出 , 菜單和編輯區(qū)域其實(shí)就是兩個(gè)單獨(dú)的 <div> , 位置、尺寸都可以隨便定義 。
使用 textareawangEditor 從 v3 版本開始不支持 textarea , 但是可以通過 onchange 來實(shí)現(xiàn) textarea 中提交富文本內(nèi)容 。
<div id="div1"><p>歡迎使用 <b>wangEditor</b> 富文本編輯器</p></div><textarea id="text1" style="width:100%; height:200px;"></textarea><script src="https://www.520longzhigu.com/uploads/allimg/220429/2101501034-2.jpg"></script><!-- 引入 wangEditor.min.js --><script type="text/javascript">const E = window.wangEditorconst editor = new E('#div1')const $text1 = $('#text1')editor.config.onchange = function (html) {// 第二步 , 監(jiān)控變化 , 同步更新到 textarea$text1.val(html)}editor.create()// 第一步 , 初始化 textarea 的值$text1.val(editor.txt.html())</script>一個(gè)頁面多個(gè)編輯器wangEditor 支持一個(gè)頁面創(chuàng)建多個(gè)編輯器 。
<head><style type="text/css">.toolbar {background-color: #f1f1f1;border: 1px solid #ccc;}.text {border: 1px solid #ccc;height: 200px;}</style></head><body><div id="div1" class="toolbar"></div><div style="padding: 5px 0; color: #ccc">中間隔離帶</div><div id="div2" class="text"><p>第一個(gè) demo(菜單和編輯器區(qū)域分開)</p></div><div id="div3"><p>第二個(gè) demo(常規(guī))</p></div><!-- 引入 wangEditor.min.js --><script type="text/javascript">const E = window.wangEditorconst editor1 = new E('#div1', '#div2')editor1.create()const editor2 = new E('#div3')editor2.create()</script></body>設(shè)置內(nèi)容以下方式中 , 如果條件允許 , 盡量使用第一種方式 , 效率最高 。
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- 模特李喬丹老公李碩是誰-網(wǎng)曝李碩系太原富二代
- 含鐵的食物有哪些含鐵豐富的食物大全
- 早中晚什么時(shí)候喝牛奶好
- vim命令文本編輯器詳解 vim下一頁
- 燉魚放啤酒好吃嗎
- 煎魚多久能熟
- 2020年福布斯中國(guó)富豪排行榜 中國(guó)首富排行榜
- 夢(mèng)見富人區(qū)_周公解夢(mèng)夢(mèng)到富人區(qū)是什么意思_做夢(mèng)夢(mèng)見富人區(qū)好不好
- kaz是哪個(gè)國(guó)家的縮寫 aze是哪個(gè)國(guó)家的縮寫
- 女生寢室群名沙雕 女生寢室群名
