quill——簡單的富文本編輯器

先介紹一下一般網頁如何實現 quill 富文本編輯器

  • 引入 quill.js 文件
<script src="https://cdn.quilljs.com/1.3.3/quill.js"></script>
  • 引入主題 css 文件
 <link href="https://cdn.quilljs.com/1.3.3/quill.snow.css" rel="stylesheet">
  • 本文操作基於 JQ,引入 JQ
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
  • 實現 HTML 文本
<!-- 編輯器 -->
<div id="editor">
  <p>Hello World!</p>
  <p>Some initial <strong>bold</strong> text</p>
  <p><br></p>
</div>
<!-- 按鈕 -->
<div style="padding: 15px;margin-top: 20px;">
  <span onclick="nihao();" class="btn">獲取編輯器內容</span>
</div>
<!-- 獲取內容 -->
<div class="content">
</div>
  • JS 初始化
var quill = new Quill('#editor', {
    theme: 'snow'
  });
  function nihao() {
    let t = quill.container.firstChild.innerHTML
    console.log(t)
    $('.content').css('display', 'block')
    $('.content').text(t)
  }

完整代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>quill富文本編輯器</title>
  <style>
    .btn{
      border:1px solid #eee;
      padding: 15px;
      margin-top: 20px;
      cursor: pointer;
      background: #00aac5;
      color: #fff;
    }
    .content{
      margin-top: 20px;
      padding:12px;
      border:1px solid #eee;
      background: #000;
      color: #fff;
      display: none;
    }
  </style>
  <script src="https://cdn.quilljs.com/1.3.3/quill.js"></script>
  <link href="https://cdn.quilljs.com/1.3.3/quill.snow.css" rel="stylesheet">
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<!-- 編輯器 -->
<div id="editor">
  <p>Hello World!</p>
  <p>Some initial <strong>bold</strong> text</p>
  <p><br></p>
</div>
<!-- 按鈕 -->
<div style="padding: 15px;margin-top: 20px;">
  <span onclick="nihao();" class="btn">獲取編輯器內容</span>
</div>
<!-- 獲取內容 -->
<div class="content">
</div>

<script>
  var quill = new Quill('#editor', {
    theme: 'snow'
  });
  function nihao() {
    let t = quill.container.firstChild.innerHTML
    console.log(t)
    $('.content').css('display', 'block')
    $('.content').text(t)
  }
</script>
</body>
</html>

項目demo 請點擊這裏

官方文檔請點擊這裏

VUE 如何實現 quill 富文本編輯器

這裏推薦一款插件 vue-quill-editor,具體代碼如下

// js
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'

export default {
  data() {
    return {
        text: ''
    }
  },
  components: {
    quillEditor
  }
}
// template
<quill-editor ref="myTextEditor" v-model.trim="text"></quill-editor>

更多內容請查看 vue-quill-editor

後續還會有 quill 富文本插件的 REACT 使用介紹,敬請期待!

發佈了47 篇原創文章 · 獲贊 32 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章