如何在 Firefox 中获取选定的文本(在 contenteditable div 中)?对于最近的版本就足够了,不需要覆盖旧版本。
假设我有一个div
如下所示的 contenteditable,有人在那里选择了一个文本,然后点击了一个按钮,我如何将选定的文本复制到剪贴板或变量?
例子:
<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 '';
}