2011-03-04 590 views
7

我正在使用以下代碼來獲取tinymce的內容。如何在tinymce中的光標位置插入圖像

tinymce_content=tinyMCE.get('txt_area_id').getContent(); 
updated_content=tinymce_content+"<img src="sample.jpg">"; 

通過使用上述代碼,我在tinymce內容中插入圖像。

問題:如何在tinymce(即)內的光標位置插入圖像如何預測光標位置並拆分內容以在獲取的內容之間插入圖像。

預先感謝

回答

18

這將在TinyMCE的編輯

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

大,每當我感到TinyMCE的鬥爭,我記得thariama謝謝 – 2011-03-04 12:03:51

+0

很高興能夠提供幫助 – Thariama 2011-03-04 12:13:08

+0

只是爲了讓這個已經很棒的帖子更清晰一點......要刷新圖片並確保它能夠正確呈現,您必須確保您在tinyMCE中創建元素DOM不是窗口DOM。所以,如果你使用'createElement'確保你做了'opener.tinyMCE.selectedInstance.getDoc()。createElement'。 – Stewart 2013-01-29 04:45:45

8

@Thariama's answer在Chrome工作得很好,我插入在所選擇的點(光標位置)的圖像節點,但不是在IE。我不得不把它修改爲以下讓它在兩個瀏覽器的工作:

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告訴我,range沒有一個insertNode方法。)

+0

這兩個解決方案不工作我。我的IE說:「SCRIPT16389:Erronãoespecificado。」這意味着「錯誤沒有在英文中指定...使用IE9和微小的3.4 – Filipiz 2012-09-15 19:05:27

+0

我使用的是tinymce 4.0。它在IE10中工作正常,但在IE11 SCRIPT16389中出現跟隨錯誤:函數不正確 文件:tinymce.min.js,行:8,專欄:20915你能幫我解決嗎? – 2014-11-19 07:14:59