JavaScript获得选中文本内容的方法

10:03:00 PM 0 Comments



在Firefox, Google Chrome, Safari, Opera中:可以用 window.getSelection() (参考MDC)
在IE下,可以用 document.selection.createRange().text (参考MSDN)
注:将两者放在一起的代码:


function getSelectionText() {
if(window.getSelection) {
return window.getSelection().toString();
} else if(document.selection && document.selection.createRange) {
return document.selection.createRange().text;
}
return '';
}

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.