![](/img/trans.png)
[英]How to set cursor position in CodeMirror in accordance to cursor position in TinyMCE 5?
[英]How To insert an image at cursor position in tinymce
我正在使用以下代碼來獲取tinymce的內容。
tinymce_content=tinyMCE.get('txt_area_id').getContent();
updated_content=tinymce_content+"<img src="sample.jpg">";
通過使用以上代碼,我將圖像插入tinymce內容中。
問題:如何在tinymce內的光標位置插入圖像(即)如何預測光標位置並拆分內容以在獲取的內容之間插入圖像。
提前致謝
這將在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
@Thariama的答案在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
方法。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.