javascript - quilljs 空格/缩进没有被保留

标签 javascript quill

嗨,我正在使用 quilljs 进行简单的富文本编辑器。 我有一个不错的工作解决方案,但我面临的一个问题是当我保存编辑器空间的内容时会丢失。 这是下面的代码,输入一些图像并在其前面留出空格,然后单击“保存”按钮以查看当前的输出。

预期:应保留空格/缩进。

代码:

<html>

<!-- Include stylesheet -->
<link href="https://cdn.quilljs.com/1.3.1/quill.snow.css" rel="stylesheet">

<!-- Create the editor container -->
<body>
<div id="editor">
  <p>Hello World!</p>
  <p>Some initial <strong>bold</strong> text</p>
  <p><br></p>
</div>
<input type=button onclick="savei()" value="save"/>
</body>
<hr>
<div id="res"></div>
</html>
<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.3.1/quill.js"></script>
<script src="img-resize.js"></script>

<!-- Initialize Quill editor -->
<script>

            var toolbarOptions = [
                ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                ['blockquote', 'code-block'],

                [{ 'header': 1 }, { 'header': 2 }],               // custom button values
                [{ 'list': 'ordered'}, { 'list': 'bullet' }],
                [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
                [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
                [{ 'direction': 'rtl' }],                         // text direction

                [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
                [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
                [ 'link', 'image', 'video', 'formula' ],          // add's image support
                [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                [{ 'font': [] }],
                [{ 'align': [] }],

                ['clean']                                         // remove formatting button
            ];

        var quill = new Quill('#editor', {
            modules: {
                toolbar: toolbarOptions
            },

            theme: 'snow'
        });


function savei(){
console.log(quill.getContents())
var x = document.getElementById("res");
x.innerHTML = quill.root.innerHTML;
console.log(x);
}
</script>

最佳答案

空白被折叠为一个空格。编辑器将 white-space 属性设置为 pre 以保留它。只需将其添加到您的 CSS 中即可:

#res {
  white-space: pre;
}

关于javascript - quilljs 空格/缩进没有被保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584115/

相关文章:

javascript - 将事件监听器添加到 Canvas 标签

angular - Ngx-quill 工具栏自定义不起作用 - quill 无法导入模块

quill - 如何在 quill.js 编辑器中添加内联注释?

JavaScript/jQuery 解析 xml

php - 如果加载时间超过 10 秒,如何刷新页面?

html - 使用 vue-quill-editor 时,Quill 将不需要的 <br> 添加到列表中

javascript - Quill 在 h1 之后添加了一个不需要的 <br>

angular - 羽毛笔编辑器中的光标位置

javascript - 如何知道用户何时从我们的网站手动删除cookies?

javascript - "document.getElementsByTagName(' 脚本 ')[0].style"或 "document.body.style"