CKEditor将HTML插入文本区域

CKEditor insert HTML into textarea

本文关键字:区域 插入文本 HTML CKEditor      更新时间:2023-09-26

我正在尝试使用javascript将文本插入CKEditor。我现在有这个脚本:

function quote_<?php echo $row['pid']; ?>() {
        var stringContent = $(".content_<?php echo $row['pid']; ?>").html();
         $("#wysiwyg").val("[quote=<?php echo $row['author']; ?>]" + stringContent + "[/quote]");
         CKEDITOR.instances.wysiwyg.insertHtml('[quote=<?php echo $row['author']; ?>]' + stringContent + '[/quote]');
}
<textarea name="message" style="width:100%" tabindex="3" rows="10" id="wysiwyg">
</textarea>

HTML没有插入到实例"wysiwyg"中,所以我无法使其工作。

有什么想法吗?

此行:

CKEDITOR.instances.wysiwyg.insertHtml('[quote={$row['author']}]' + stringContent + '[/quote]');

"author"周围的单引号没有转义。尝试:

CKEDITOR.instances.wysiwyg.insertHtml("[quote={$row['author']}]" + stringContent + "[/quote]");

这对我有效:

CKEDITOR.instances.TEXTATEA_ID.insertHtml('<p> html here. </p>');

您可以始终将javascript代码嵌入到.php文件中,如下所示:

<?php
echo <<<JS
<script type='text/javascript'>
    function quote_{$row['pid']}() {
        var stringContent = $(".content_{$row['pid']}").html();
            $("#wysiwyg").val("[quote={$row['author']}]" + stringContent + "[/quote]");
        CKEDITOR.instances.wysiwyg.insertHtml('[quote={$row['author']}]' + stringContent + '[/quote]');
}
</script>
    <textarea name="message" style="width:100%" tabindex="3" rows="10" id="wysiwyg">
    </textarea>
JS;
?>

此外,您也可以使用php文件,在这些文件中保留纯(X)HTML标记,但当您达到通常要回显的程度时,只需执行以下操作:

var stringContent = $(".content_"+<?= $row['pid']; ?>).html();