简体   繁体   English

ckeditor - 不在textarea中设置html数据

[英]ckeditor - not setting html data inside textarea

I am trying to use ckeditor with cakephp and I've written a helper . 我正在尝试使用带有cakephp的 ckeditor ,我写了一个帮手 The problem is when I enter some line breaks or add checkboxes (or other html elements) with CKeditor, the editor crashes the next time I edit the content. 问题是当我输入一些换行符或用CKeditor添加复选框(或其他html元素)时,编辑器会在下次编辑内容时崩溃。 Firefox returns the following error: Firefox返回以下错误:

SyntaxError: unterminated string literal SyntaxError:未终止的字符串文字

and highlights }).setData("<p>test</p> from the section below: 并突出显示}).setData("<p>test</p>来自以下部分:

<script type="text/javascript">CKEDITOR.replace('data[Template][body]',{
            toolbar: '',
            toolbarGroups: '',
            width: 950,
            height: 500
    }).setData("<p>test</p>

<p>&nbsp;</p>

<p>test</p>");</script>

Here is the code in the cake helper: 这是蛋糕助手中的代码:

$code = "CKEDITOR.replace('{$id}',{
            {$options['toolbar']},
            {$options['toolbarGroups']},
            width: {$options['width']},
            height: {$options['height']}
}).setData('" . trim($value). "');";

return $this->Javascript->codeBlock($code);

Any help is greatly appreciated. 任何帮助是极大的赞赏。

This is because you have linebreaks inside JavaScript string. 这是因为您在JavaScript字符串中有换行符。 You should output line breaks to HTML as "\\n" so your HTML output would be like: 您应该将换行符输出为“\\ n”,以便HTML输出如下:

<script type="text/javascript">CKEDITOR.replace('data[Template][body]',{
        toolbar: '',
        toolbarGroups: '',
        width: 950,
        height: 500
}).setData("<p>test</p>\n<p>&nbsp;</p>\n<p>test</p>");</script>

So inside your helper: 所以在你的助手里面:

$out .= "}).setData('" .  str_replace("\n", '\n', $value). "');";

I've used single quotes so it will print out \\n instead of line break; 我使用单引号,因此它将打印出\\ n而不是换行符;

Or you can use: str_replace("\\n", "\\\\n", $value) 或者您可以使用: str_replace("\\n", "\\\\n", $value)

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

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