[英]Insert text at the caret position, inside a contenteditable body of a frame
I have an iframe object, and there is a contenteditable body inside. 我有一个iframe对象,并且内部有一个contenteditable主体。 I would like to paste some text, or html element at the caret position.
我想在插入符号的位置粘贴一些文本或html元素。 I tried this code, but I've got an error
Cannot read property 'createRange' of undefined
我尝试了这段代码,但出现错误
Cannot read property 'createRange' of undefined
$('#edit_frame').selection.createRange().pasteHTML($('<span>text</span>'));
Because you need get the iFrame's document. 因为您需要获取iFrame的文档。
// HTML selected
document.getElementById('edit_frame').contentWindow.document.selection.createRange().htmlText;
But it's IE only. 但这只是IE。 Read the @Tim Down answer: https://stackoverflow.com/a/6668159/2389232 .
阅读@Tim Down答案: https ://stackoverflow.com/a/6668159/2389232。
I have adapted the function to get selection HTML for iFrames too. 我已经改编了该功能,以获取iFrame的选择HTML。 See http://jsfiddle.net/4bp42891/ .
参见http://jsfiddle.net/4bp42891/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.