append an image to ckeditor via jquery

依然范特西╮ 提交于 2019-12-24 09:48:54

问题


I'm using ckeditor to add posts at a site:

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

and I want to append some text or image to the textarea of it when I click at a button via jquery.

I tried this but not worked:

<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>

thank you.


回答1:


Use the 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>



回答2:


Use this :

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



回答3:


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

It looks like you've got an extra quote in there and I think you might need to make it a jquery object. Make sure to use var keyword to keep the variable local.



来源:https://stackoverflow.com/questions/16574375/append-an-image-to-ckeditor-via-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!