2013-05-15 25 views
0

我使用的CKEditor在一個站點添加職位,CKEDITOR:附加的圖像通過jQuery

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

,我想追加一些文字或圖像到它的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> 

謝謝。

回答

1

使用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> 
+0

這個工作就像一個魅力。非常感謝你 –

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

它看起來像你有一個額外的報價在那裏,我想你可能需要使它成爲一個jQuery對象。請務必使用var關鍵字將本地變量保留。

1

使用此:

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