0

我在项目中使用 jquery 和 CKEditor。我希望用户可以在 CKEditor 中编写/编辑他们的 html 代码。当我在编辑器中输入多个 textarea 标签时,它们看起来很好,因为它们都留在 textarea 内,如下所示,

在此处输入图像描述

然后我保存它,我确认它保存正确,因为我检查了文件中的源代码。

但是,如果我再次在 CKEditor 中打开文件,则无法正确显示,如下所示,

在此处输入图像描述

以下是我使用的示例代码。虽然它不是完整的代码。

      <textarea id="eidtArea" name="editScriptContent"><!--Load the saved file here (I will skip this part here)--></textarea>
        <script type="text/javascript">
               CKEDITOR.replace( 'eidtArea');
        </script>   

我的问题是:

    1. 我是否错过了在此插件中设置的任何内容,或者我可以做些什么?
    2. 事实上,我真的不需要使用这个插件。我可以简单地使用 textarea 让用户编辑代码。基本上我想使用这个插件的主要原因是因为我希望用户可以在编辑器中创建 textarea 标签。但是,如果我只是使用 textarea 标签作为编辑器,他们就无法在此编辑器中添加任何其他 textarea 标签。这种方法是否有任何解决方法?如果是这样,那么在这种情况下我确实需要使用 CKEditor。
4

1 回答 1

0

您必须先对 HTML 进行编码,然后才能以<textarea>. 否则你的后端会产生这个:

<textarea id="editor">
    <p>Some text: <textarea>foo</textarea></p>
    <p>Some text: <textarea>bar</textarea></p>
</textarea>

浏览器会将其解析为:

<textarea id="editor">
    <p>Some text: <textarea>foo
</textarea>
// And here the rest of the content of the editor,
// but outside the editor's textarea

因此,您至少需要将所有<字符替换为&lt;. 例如,如果您使用 PHP,您应该通过htmlspecialchars()函数传递内容。

于 2014-11-11T00:29:54.230 回答