首页 > web前端 > js教程 > 正文

使用Quill.js构建文本编辑器

WBOY
发布: 2023-08-23 23:29:02
转载
1235 人浏览过

使用Quill.js构建文本编辑器

Quill是一款免费且开源的文本编辑器,属于所见即所得编辑器的范畴,主要用于我们今天使用的现代网络。它是一个高度可定制的文本编辑器,具有许多表达性的API。Quill非常易于使用,并提供了一个良好的界面,即使对于只有标记经验的人来说,也很容易理解。

在本教程中,我们将使用多个示例来解释如何使用Quill.js构建文本编辑器。

虽然有许多所属于所见即所得文本编辑器的富文本编辑器,但最广泛使用的是Quill,差距非常大。现在,让我们来学习如何使用Quill。

让我们使用Quill创建一个基本的文本编辑器

与Quill一起工作的第一步是能够在我们选择的编辑器中使用它,并为此,我们需要将下面显示的两个CDN链接放在我们的HTML代码的

标签中。
<link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
<script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
登录后复制

第一个CDN链接是Quill的CSS样式文件,而第二个CDN链接是Quill的JavaScript文件。我们需要将上面显示的这两行代码添加到我们的HTML代码的标签中。

我们的标签应该看起来像这样。

<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Quill Text Editor</title>
   <link href="https://cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
   <script src="https://cdn.quilljs.com/1.3.7/quill.js"></script>
</head>
登录后复制

现在我们已经在标签中添加了CDN,现在是时候开始处理标签了。在标签内部,让我们创建一个id="editor"的

标签,并添加一些指定高度的简单样式。请参考下面显示的标签

<body>
   <div id="editor" style="height: 250px"></div>
</body>
登录后复制
在上面的代码中,我们创建了一个id为"editor"的
标签,并提供了一个简单的样式,指定了的高度为250px

现在剩下的就是在其中创建一个<script>标签,我们将在其中创建一个Quill类的实例,然后将我们创建的<div>的id作为第一个参数传递,第二个参数基本上是一个对象,我们在文本编辑器中指定对象的属性。</p> <p>考虑下面显示的<b><script>标签</b>。</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>&lt;script&gt; var quill = new Quill('#editor', { theme: 'snow' }); &lt;/script&gt; </pre><div class="contentsignin">登录后复制</div></div> <p>上述的<script>标签应该放在<body>标签的末尾,即在<body>标签关闭之前。</p> <h3>index.html</h3> <p>整个HTML代码如下所示。</p> <h3>Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> <script> var quill = new Quill('#editor', { theme: 'snow' }); </script>

登录后复制

如果您在浏览器中打开上述HTML文件,您将在浏览器中看到一个文本编辑器输出。在您将看到的文本编辑器中,我们将有大量的工具栏选项可供我们使用,我们可以在文本编辑器中使用任何一个。

让我们自定义文本编辑器的外观

现在假设我们只想提供两个默认的工具栏选项,而不是在普通文本编辑器中默认获得的所有选项。在这种情况下,我们可以使用下面显示的<script>标签。</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>&lt;script&gt; let toolbarOptions = [ ['bold', 'italic', 'underline'] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); &lt;/script&gt; </pre><div class="contentsignin">登录后复制</div></div> <p>在上面的<script>标签中,我们只提供了三个选项,即粗体、斜体和下划线,在工具栏中,因此只有这些选项将对文本编辑器可用。</p> <p><b>index.html</b></p> <p>下面显示的是更新后的 index.html 文件。</p> <h3>Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> <script> var toolbarOptions = [ ['bold', 'italic', 'underline'] ] var quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); </script>

登录后复制

如果您在浏览器中运行上述文件,您将只能在文本编辑器中看到三个工具栏选项,即粗体选项、斜体选项和下划线选项。

在控制台中记录文本编辑器的内容

现在假设我们想要将我们在文本编辑器中写的内容记录到控制台中,为了做到这一点,我们首先需要在标签中创建一个按钮。

考虑下面显示的创建按钮的代码片段。

<button onclick="consoleHTMLContent()">Print in Console</button>
登录后复制

现在让我们专注于<script>标签,其中我们需要创建一个函数,该函数将实际记录quill文本编辑器的内容以及一些其他工具栏选项。</p> <p>考虑下面显示的更新的<script>标签。</p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'>&lt;script&gt; let toolbarOptions = [ ['bold', 'italic', 'underline'],[{ 'size': ['small', false, 'large', 'huge'] }],[{ 'color': [] }, { 'background': [] }] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); function consoleHTMLContent() { console.log(quill.root.innerHTML); } &lt;/script&gt; </pre><div class="contentsignin">登录后复制</div></div> 在上述<script>标签中,我们有一个名为consoleHTMLContent的函数,其中我打印了quill对象的root属性中存在的内容 <p><b>index.html</b></p> <p>更新后的<b>index.html</b>代码如下所示。</p> <h3>Example</h3> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:javascript;toolbar:false;'><!DOCTYPE html> <html lang="en"> &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt; &lt;title&gt;Quill Text Editor&lt;/title&gt; &lt;link href=&quot;https://cdn.quilljs.com/1.3.7/quill.snow.css&quot; rel=&quot;stylesheet&quot;&gt; &lt;script src=&quot;https://cdn.quilljs.com/1.3.7/quill.js&quot;&gt;&lt;/script&gt; &lt;/head&gt; <body> <div id="editor" style="height: 200px"></div> &lt;button onclick=&quot;consoleHTMLContent()&quot;&gt;Print in Console&lt;/button&gt; <script> let toolbarOptions = [ ['bold', 'italic', 'underline'],[{ 'size': ['small', false, 'large', 'huge'] }],[{ 'color': [] }, { 'background': [] }] ] let quill = new Quill('#editor', { modules: { toolbar: toolbarOptions }, theme: 'snow' }); function consoleHTMLContent() { console.log(quill.root.innerHTML); } </script>

登录后复制

如果我们在浏览器中运行上述代码,我们将会看到一个文本编辑器,一旦我们在编辑器中输入一些文本并点击按钮,quill文本编辑器的根对象将会在控制台中打印出来。

输出

我尝试在编辑器中写了一行简单的代码,然后点击了按钮,这是我在浏览器控制台中得到的输出。

<p>Hi There <strong>Inside HTML </strong><em>Is this italic?</em></p>
登录后复制

结论

在本教程中,我们演示了如何使用Quill.js创建具有不同工具栏选项的文本编辑器。通过多个示例,我们解释了如何添加或删除工具栏,以及如何在Quill文本编辑器中控制根元素。

以上是使用Quill.js构建文本编辑器的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!