问题
I want to insert an image in textarea with jquery. (I know that img tag could not to inserted in textarea). Im using tinymce
<img src="image.jpg" class="po"/>
<form>
<input type="text" name="yassi" class="infobox"/>
<br />
<textarea class="me"></textarea>
<input type="submit" value="click" class="submit"/>
</form>
jquery:
<script type="text/javascript">
tinyMCE.init({
mode : "textareas",
theme : "simple"
});
</script>
<script type="text/javascript" scr="config.js">
</script>
and in config.js I have:
$(document).ready(function(){
var sr = $('.po').attr('src');
$('.po').click(function(){
$('.mceContentBody').append('<img src="'+sr+'"/>');
});});
When clicking, image couldnt insert in textarea. How can do this? Thanks in advance.
回答1:
You need to call the mceInsertContent command, like this:
tinyMCE.execCommand('mceInsertContent',false,'<img src="'+sr+'"/>');
If you switch to the jQuery plugin version, it'd look like this:
$('.mceContentBody').tinymce().execCommand('mceInsertContent',false,'<img src="'+sr+'"/>');
来源:https://stackoverflow.com/questions/3918773/textarea-tinymce-and-insert-an-image-in-textarea