简体   繁体   English

如何捕获在富文本编辑器 div 中键入的内容 - Quill 富文本编辑器

[英]How to capture content typed in a rich text editor div - Quill rich editor

I am trying to fetch content that has been typed in the editor div of quill editor http://quilljs.com/examples/ using jquery codes but it does not seem to work.我正在尝试使用 jquery 代码获取在 quill 编辑器http://quilljs.com/examples/的编辑器 div 中输入的内容,但它似乎不起作用。 It works for other text inputs but not for the editor div.它适用于其他文本输入,但不适用于编辑器 div。 I have a running illustration below .我在下面有一个正在运行的插图。

 $(function(){ $(document).on("click", "#SubmitButton", function(e) { e.preventDefault(); { var question = $("#question").val(); var title = $("#title").val(); alert (question); alert (title); e.preventDefault(); } }); }); advancedEditor = new Quill('#question', { modules: { 'toolbar': { container: '#toolbar' }, 'link-tooltip': true, 'image-tooltip': true, 'multi-cursor': true }, styles: false, theme: 'snow' });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.quilljs.com/0.20.1/quill.js"></script> <link href="http://cdn.quilljs.com/0.20.1/quill.snow.css" rel="stylesheet"/> <form id="postform" name="postform" action=""> Title <input type="text" name="title" id="title"> <br> <div class="advanced-wrapper" id="qs" style="width:95%; padding:0px; margin:0px;"> <div class="toolbar-container" id="toolbar"><span class="ql-format-group"> <select title="Font" class="ql-font"> <option value="sans-serif" selected>Sans Serif</option> <option value="Georgia, serif">Serif</option> <option value="Monaco, 'Courier New', monospace">Monospace</option> </select> <select title="Size" class="ql-size"> <option value="10px">Small</option> <option value="13px" selected>Normal</option> <option value="18px">Large</option> <option value="32px">Huge</option> </select></span><span class="ql-format-group"><span title="Bold" class="ql-format-button ql-bold"></span><span class="ql-format-separator"></span><span title="Italic" class="ql-format-button ql-italic"></span><span class="ql-format-separator"></span><span title="Underline" class="ql-format-button ql-underline"></span></span><span class="ql-format-group"> <select title="Text Color" class="ql-color"> <option value="rgb(187, 187, 187)"></option> <option value="rgb(161, 0, 0)"></option> </select><span class="ql-format-separator"></span> <select title="Background Color" class="ql-background"> <option value="rgb(0, 0, 0)"></option> <option value="rgb(230, 0, 0)"></option> </select><span class="ql-format-separator"></span> <select title="Text Alignment" class="ql-align"> <option value="left" selected></option> <option value="center"></option> <option value="right"></option> <option value="justify"></option> </select></span><span class="ql-format-group"><span title="Link" class="ql-format-button ql-link"></span><span class="ql-format-separator"></span><span title="Image" class="ql-format-button ql-image"></span><span class="ql-format-separator"></span><span title="List" class="ql-format-button ql-list"></span></span></div> <div id="question" class="editor-container"></div> </div> <input type="submit" class="btn btn-info" id="SubmitButton" value="Post Your Question" /> </form>

I want when i click the submit button, It captures also the content typed in the text editor div and alerts the values types.我希望当我单击提交按钮时,它还捕获在文本编辑器 div 中键入的内容并提醒值类型。

Any help will be appreciated任何帮助将不胜感激

Quill has it's own API for content retrieval. Quill 有自己的 API 用于内容检索。 Replacing your click method with following code should allow you to get the plain text value from your Quill instance.用下面的代码替换你的 click 方法应该允许你从你的 Quill 实例中获取纯文本值。

$(document).on("click", "#SubmitButton", function(e) {
  e.preventDefault();   
  var question = advancedEditor.getText();
  var title = $("#title").val();    
  console.log(title, question);
});

Here's a link to Quill's documentation for retrieval methods这是 Quill 检索方法文档的链接

First add all the libraries for quilljs and use getText method to get the contents.首先添加 quilljs 的所有库并使用 getText 方法获取内容。

    <!-- Theme included stylesheets -->
    <link href="//cdn.quilljs.com/1.3.0/quill.snow.css" rel="stylesheet">
    <link href="//cdn.quilljs.com/1.3.0/quill.bubble.css" rel="stylesheet">

    <!-- Core build with no theme, formatting, non-essential modules -->
    <link href="//cdn.quilljs.com/1.3.0/quill.core.css" rel="stylesheet">
    <script src="//cdn.quilljs.com/1.3.0/quill.core.js"></script>
    <!-- Include stylesheet -->
    <link href="https://cdn.quilljs.com/1.3.0/quill.snow.css" rel="stylesheet">
    <script src="https://cdn.quilljs.com/1.3.0/quill.js"></script>
    <script>
    document.getElementById("myBtn").addEventListener("click", function(){
        var text =quill.getText( 0, 50); 
    alert(text);
    });
    </script>

I see the question is jQuery-oriented, but the JavaScript concept is fairly parallel.我看到这个问题是面向 jQuery 的,但 JavaScript 的概念是相当平行的。 Here is how I did it, for you Express folks.这就是我如何做到的,为你们 Express 的人。 It seems to have worked very well in conjunction with express-sanitizer.它似乎与 express-sanitizer 结合使用效果很好。

app.js应用程序.js

 import expressSanitizer from 'express-sanitizer'

 app.use(expressSanitizer())

 app.post('/route', async (req, res) => {
     const title = req.body.article.title
     const content = req.sanitize(req.body.article.content)
     // Do stuff with content
 })

new.ejs新的.ejs

 <head>
     <link href="https://cdn.quilljs.com/1.3.2/quill.snow.css" rel="stylesheet">
 </head>

 ...

 <form action="/route" method="POST">
     <input type="text" name="article[title]" placeholder="Enter Title">
     <div id="editor"></div>
     <input type="submit" onclick="return quillContents()" />
 </form>

 ...

 <script src="https://cdn.quilljs.com/1.3.2/quill.js"></script>
 <script>
     const quill = new Quill('#editor', {
         theme: 'snow'
     })

     const quillContents = () => {
         const form = document.forms[0]
         const editor = document.createElement('input')

         editor.type = 'hidden'
         editor.name = 'article[content]'
         editor.value = document.querySelector('.ql-editor').innerHTML
         form.appendChild(editor)

         return form.submit()
     }
</script>

express-sanitizer ( https://www.npmjs.com/package/express-sanitizer ) express-sanitizer ( https://www.npmjs.com/package/express-sanitizer )

document.forms (https://developer.mozilla.org/en-US/docs/Web/API/Document/forms ) document.forms (https://developer.mozilla.org/en-US/docs/Web/API/Document/forms )

My view only has one form, so I used document.forms[0] , but if you have multiple or may extend your view in the future to have multiple forms, check out the MDN reference.我的视图只有一个表单,所以我使用了document.forms[0] ,但是如果您有多个或将来可能扩展您的视图以拥有多个表单,请查看 MDN 参考。

What we are doing here is creating a hidden form input that we assign the contents of the Quill Div, and then we bootleg the form submit and pass it through our function to finish it off.我们在这里做的是创建一个隐藏的表单输入,我们分配 Quill Div 的内容,然后我们盗用表单提交并通过我们的函数传递它以完成它。

Now, to test it, make a post with <script>alert()</script> in it, and you won't have to worry about injection exploits.现在,要测试它,请使用<script>alert()</script>发布一个帖子,您就不必担心注入漏洞了。

That's all there is to it.这就是它的全部内容。

What you want to focus on, in my opinion, is document.querySelector('.ql-editor').innerHTML Punch that into your console.log() and you will see the HTML.在我看来,您想要关注的是document.querySelector('.ql-editor').innerHTML其打入您的console.log() ,您将看到 HTML。

My solution for version 1.2.2 is:我对 1.2.2 版的解决方案是:

First import css and js in HEAD首先在 HEAD 中导入 css 和 js

<link href="https://cdn.quilljs.com/1.2.2/quill.snow.css" rel="stylesheet" />
<script src="https://cdn.quilljs.com/1.2.2/quill.js"></script>

In your HTML body declare a div with id在你的 HTML 正文中声明一个带有 id 的 div

<div id="content_email" name="content_email"></div>

In your scripts section declare the editor在您的脚本部分声明编辑器

<script>
  var quill = new Quill('#content_email', {
    theme: 'snow'
  });
</script>

And finally to get the content with HTML tags, put this inside an onclick function:最后,要获取带有 HTML 标签的内容,请将其放入 onclick 函数中:

var content = document.querySelector('.ql-editor').innerHTML;
console.log("content: ", content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM