Firefox で (contenteditable div で) 選択したテキストを取得するにはどうすればよいですか? 最近のバージョンで十分です。古いバージョンをカバーする必要はありません。
以下のようなcontenteditablediv
があり、誰かがそこでテキストを選択してボタンを押したとします。選択したテキストをクリップボードまたは変数にコピーするにはどうすればよいですか?
例:
<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 '';
}