簡體   English   中英

在框架的可編輯內容內的插入符號位置插入文本

[英]Insert text at the caret position, inside a contenteditable body of a frame

我有一個iframe對象,並且內部有一個contenteditable主體。 我想在插入符號的位置粘貼一些文本或html元素。 我嘗試了這段代碼,但出現錯誤Cannot read property 'createRange' of undefined

$('#edit_frame').selection.createRange().pasteHTML($('<span>text</span>'));

因為您需要獲取iFrame的文檔。

// HTML selected
document.getElementById('edit_frame').contentWindow.document.selection.createRange().htmlText; 

但這只是IE。 閱讀@Tim Down答案: https ://stackoverflow.com/a/6668159/2389232。

我已經改編了該功能,以獲取iFrame的選擇HTML。 參見http://jsfiddle.net/4bp42891/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM