javascript CKEditor 将 HTML 插入文本区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9981216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CKEditor insert HTML into textarea
提问by bear
I'm trying to insert text into CKEditor using javascript. I have currently got this script:
我正在尝试使用 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 is not being inserted into the instance 'wysiwyg', so I can't get this to work.
HTML 没有被插入到实例“所见即所得”中,所以我无法让它工作。
Any ideas?
有任何想法吗?
回答by steve_c
This row:
这一行:
CKEDITOR.instances.wysiwyg.insertHtml('[quote={$row['author']}]' + stringContent + '[/quote]');
the single quotes around 'author' are not escaped. Try:
'author' 周围的单引号不会被转义。尝试:
CKEDITOR.instances.wysiwyg.insertHtml("[quote={$row['author']}]" + stringContent + "[/quote]");
回答by Memonic
This worked for me:
这对我有用:
CKEDITOR.instances.TEXTATEA_ID.insertHtml('<p> html here. </p>');
回答by Victor Ni?u
You can always embed your javascript code into a .php file, as it follows:
您始终可以将 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;
?>
Also, you can alternatively use php files in which you keep pure (X)HTML markup, but when you come to the point where you would normally echo, just do that:
此外,您也可以使用 php 文件,在其中保留纯 (X)HTML 标记,但是当您达到通常会回显的程度时,只需执行以下操作:
var stringContent = $(".content_"+<?= $row['pid']; ?>).html();