JavaScript/jQuery:如何在Firefox中获取选定的文本 [英] JavaScript / jQuery: how to get selected text in Firefox
本文介绍了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();
这篇关于JavaScript/jQuery:如何在Firefox中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文