简体   繁体   English

在框架的可编辑内容内的插入符号位置插入文本

[英]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.

相关问题 在插入符号处插入文本 position Contenteditable:Angular 8 - Insert text at caret position Contenteditable : Angular 8 在contenteditable div中的插入符号中插入文本 - Insert text at caret in contenteditable div 如何在特定位置的contenteditable div中添加/插入/更新文本? - How to add / insert / update text inside contenteditable div at a particular position? 链接内的 Contenteditable 块,如何防止重定向但让文本选择\插入符号位置发生变化? - Contenteditable block inside link, how to prevent redirect but let text selection\caret position changes? 获取内容Editable caret position - Get contentEditable caret position 在插入符号IE 8的插入位置插入文本 - Insert text at caret position IE 8 在Chrome的contenteditable元素的节点内将插入符位置设置为零 - Set caret position to zero inside node of contenteditable element in chrome 将插入符号位置设置为 contentEditable 元素内的空节点 - Setting the caret position to an empty node inside a contentEditable element 修改contenteditable div中的文本而不重置插入符(光标)位置 - Modify text in a contenteditable div without resetting the caret (cursor) position 在contenteditable div中设置插入符位置 - Set caret position in contenteditable div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM