dongpigui8898 2013-09-25 12:29
浏览 64
已采纳

ckeditor - 不在textarea中设置html数据

I am trying to use ckeditor with cakephp and I've written a helper. 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. Firefox returns the following error:

SyntaxError: unterminated string literal

and highlights }).setData("<p>test</p> from the section below:

<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.

  • 写回答

1条回答 默认 最新

  • doushibu2453 2013-09-25 13:11
    关注

    This is because you have linebreaks inside JavaScript string. You should output line breaks to HTML as " " so your HTML output would be like:

    <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>
    

    So inside your helper:

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

    I've used single quotes so it will print out instead of line break;

    Or you can use: str_replace(" ", "\ ", $value)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?