JavaScript/jQuery:如何在Firefox中获取选定的文本
问题描述:
如何在Firefox中获取选定的文本(在可内容编辑的div中)?对于最新版本就足够了,无需覆盖旧版本.
how can I get the selected text (in a contenteditable div) in Firefox ? It would be enough for recent versions, no need to cover old versions.
说我有一个内容可编辑的div
,如下所示,有人在那里选择了文本,然后按了一个按钮,我该如何将所选文本复制到剪贴板或变量中?
Say I have a contenteditable div
that looks like the below and someone selects a text there and then hits a button, how can I copy the selected text to the clipboard or a variable ?
示例:
<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 '';
}
谢谢您的帮助,蒂姆.
答
var selectedText = "" + window.getSelection();