CKEditor无法显示textarea的value属性内容,如何解决?
问题原因
<textarea> 标签的内容并非通过 value 属性定义,它的实际内容需要嵌套在 <textarea> 和 </textarea> 标签之间。你现在把数据库取出的内容放在 value 属性里,CKEditor 无法识别读取这部分内容,导致编辑器显示为空。
修复方法
将原本写在 value 属性中的 PHP 输出代码,直接移到 <textarea> 标签内部即可:
<script src="https://cdn.ckeditor.com/ckeditor5/34.2.0/classic/ckeditor.js"></script> <textarea id="editor" rows="6" name="post-body"><?php echo html_entity_decode($post['content']) ?></textarea> <script> ClassicEditor .create( document.querySelector( '#editor' ) ) .catch( error => { console.error( error ); } ); </script>
补充说明
- 确保数据库中存储的
$post['content']是经过htmlentities()转义后的数据,这样html_entity_decode()才能正确还原成 CKEditor 可解析的 HTML 内容。 - 如果内容包含
<、>这类特殊字符,html_entity_decode()会将其还原为原始符号,CKEditor 就能正常加载并显示内容。
内容的提问来源于stack exchange,提问作者c.dasa
相关产品推荐
相关产品推荐

