将数据插入tinymmce的正确方法不是将其打印出来,甚至不使用。请考虑以下代码htmlentities
<div class="editor" id="editor">
</div>
<script>
tinymce.init({
selector: 'div.editor',
theme: 'inlite',
plugins: 'image table link paste contextmenu textpattern autolink',
insert_toolbar: 'quickimage quicktable',
selection_toolbar: 'bold italic | quicklink h1 h2 h3 blockquote',
inline: true,
paste_data_images: true,
automatic_uploads: true,
init_instance_callback : function(ed) {
// ed.setContent("<h1>Title</h1><p>Content...</p>");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
ed.setContent(xhttp.responseText);
}
};
xhttp.open("GET", "content.php", true);
xhttp.send();
},
content_css: [
'//www.tinymce.com/css/codepen.min.css'
]
});
</script>
文件应该只是简单地打印html内容content.php
<?php
$content = ''; // Fetch from database
print $content;
?>
注意在我初始化tinymce时的函数。这是在 tinymce 初始化后调用的函数。现在,直接在编辑器内部使用,在内部进行ajax调用并在那里呈现它。我已经放入了一个示例注释行,只是为了帮助您解决相同的问题。这也将负责安全验证(如果有的话,则不执行脚本标记)。init_instance_callback
print
init_instance_callback
同时获取编辑器的内容,同时将其保存到数据库是
var content = tinyMCE.get('editor').getContent();
然后,您可以发出ajax post请求以将数据保存到数据库。
现在我为什么使用ajax很重要。我尝试了很多其他方法,我可以直接打印它。但这会导致安全漏洞,因为脚本标签可以在编辑器初始化之前运行。
我在此示例中使用了 div,但即使对于文本区域也是如此。也不要使用htmlentities,因为这会转义html内容,并且你希望看到在tinymmce中呈现的内容,而不是转义版本。