【问题标题】:CKEDITOR values show in 1 line in the console log resultCKEDITOR 值显示在控制台日志结果的 1 行中
【发布时间】:2023-03-03 02:32:02
【问题描述】:

我正在创建 CKeditor 函数。现在我的问题是我无法在 CKeditor 文本区域中获取值。我尝试控制台日志显示结果,它不能跟随 html 显示如下图:

下面是我的代码:

<textarea id="editor1" name="editor1">This is sample text</textarea>

<script type="text/javascript">
CKEDITOR.replace( 'editor1', {
            height: 300,
           // enterMode: CKEDITOR.ENTER_BR,
            filebrowserUploadUrl: "/eokclaim/app/ajaxfile.php?type=file",
            filebrowserImageUploadUrl: "/eokclaim/app/ajaxfile.php?type=image"
        } );

    function updateDiv(){
       var editorText = CKEDITOR.instances.editor1.getData();
       console.log(editorText);
    }
</script>

我希望控制台日志中的预期结果是(我希望控制台日志结果显示在 1 行):

<p>Picture 1:</p><br /><p><img alt="" src="https://www.abc.com.my/folder/app/uploads/blue-pin.png" style="height:100px; width:100px" /></p><br /><p>Picture 2:</p><br /><p><img alt="" src="https://www.abc.com.my/folder/app/uploads/1.png" style="height:34px; width:100px" /></p>

希望有人可以指导我如何解决问题。谢谢。

【问题讨论】:

  • 如果 editorText 是一个字符串,您是否尝试过简单地删除换行符?喜欢editorText.replace(/(\r\n|\n|\r)/gm, "");
  • 我想将 html 保存在变量中,然后传递给后端
  • 你问如何在一行中显示多行文本(这个html是文本)。我认为我的第一条评论回答了这个问题。您仍然可以将其保存在变量中并将其传递给后端。
  • 好的。我可以知道如何在我的代码中添加您的代码吗?
  • 我会把它作为答案发布。

标签: javascript html ckeditor


【解决方案1】:

为了删除换行符,您可以使用replace 函数。您的代码如下所示:

function updateDiv(){
   var editorText = CKEDITOR.instances.editor1.getData().replace(/(\r\n|\n|\r)/gm, "");
   console.log(editorText);
}

如果getData() 像我想的那样返回一个字符串,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    相关资源
    最近更新 更多