【发布时间】:2014-01-01 00:25:37
【问题描述】:
如何在 Firefox 中获取选定的文本(在 contenteditable div 中)?最新版本就足够了,不需要覆盖旧版本。
假设我有一个 contenteditable div,如下所示,有人在那里选择了一个文本,然后点击了一个按钮,我如何将选定的文本复制到剪贴板或变量?
示例:
<div class='editInput' id='editInput'>Some awesome text</div>
我当前的功能(在 IE 中工作):
function GetSelection()
{
if (typeof window.getSelection != "undefined")
{
var sel = window.getSelection();
if (sel.rangeCount)
{
var container = document.createElement('div');
for (var i = 0, len = sel.rangeCount; i < len; ++i)
container.appendChild(sel.getRangeAt(i).cloneContents());
return container.innerHTML;
}
}
else if (typeof document.selection != 'undefined')
if (document.selection.type == 'Text')
return document.selection.createRange().htmlText;
return '';
}
【问题讨论】:
标签: javascript firefox contenteditable textselection getselection