How To insert an image at cursor position in tinymce

前端 未结 2 1481
独厮守ぢ
独厮守ぢ 2020-12-15 00:58

I am using following codes to fetch the conent of tinymce.

tinymce_content=tinyMCE.get(\'txt_area_id\').getContent();
updated_content=tinymce_content+\"

        
相关标签:
2条回答
  • 2020-12-15 01:21

    @Thariama's answer worked fine for me in Chrome, but not in IE. I had to modify it to the following to get it to work in both browsers:

    var ed = tinyMCE.get('txt_area_id');                // get editor instance
    var newNode = ed.getDoc().createElement ( "img" );  // create img node
    newNode.src="sample.jpg";                           // add src attribute
    ed.execCommand('mceInsertContent', false, newNode.outerHTML)
    

    (IE was telling me that range did not have an insertNode method.)

    0 讨论(0)
  • 2020-12-15 01:25

    This will insert an image node at the selected spot (cursor position) in a tinymce editor

    var ed = tinyMCE.get('txt_area_id');                // get editor instance
    var range = ed.selection.getRng();                  // get range
    var newNode = ed.getDoc().createElement ( "img" );  // create img node
    newNode.src="sample.jpg";                           // add src attribute
    range.insertNode(newNode);                          // insert Node
    
    0 讨论(0)
提交回复
热议问题