通过jquery将图像附加到ckeditor

时间:2013-05-15 20:28:32

标签: jquery ckeditor

我正在使用ckeditor在网站上添加帖子:

<textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10"></textarea>

当我通过jquery点击按钮时,我想将一些文字或图像附加到它的文本区域。

我尝试了这个但没有奏效:

<script>
$(document).ready(function(){
$('.button').click(function(){
  img = "<img src='http://localhost/sdn/files/uploads/1368647314.png'/>'";
  $(".cke_editable").append(img); // also I tried:  $("#editor1").append(img);
});
});
</script>
谢谢。

3 个答案:

答案 0 :(得分:2)

使用CKEditor API

<script>
$(document).ready(function(){
$('.button').click(function(){
  img = "<img src='http://localhost/sdn/files/uploads/1368647314.png'/>'";
  CKEDITOR.instances.editor1.insertHtml(img);
});
});
</script>

答案 1 :(得分:2)

使用此:

<script>
$(document).ready(function(){
$('.button').click(function(){
  img = "<img src='http://localhost/sdn/files/uploads/1368647314.png'/>'";
  CKEDITOR.instances.editor1.setData(img);
});
});
</script>

答案 2 :(得分:1)

var img=$("<img src='http://localhost/sdn/files/uploads/1368647314.png'/>");

看起来你在那里有一个额外的引用,我想你可能需要把它作为一个jquery对象。请务必使用var关键字将变量保持为本地。