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