使用window.getSelection()獲取div中選中文字內容及位置
window.getSelection().toString() //選中內容
window.getSelection().anchorOffset; //開始位置
window.getSelection().focusOffset; //結束位置
如果需要兼容ie9以下:
if (window.getSelection) {
// window.getSelection().toString();
// window.getSelection().anchorOffset; //開始位置
// window.getSelection().focusOffset; //結束位置
} else if (document.selection && document.selection.type != "Control") {
//ie9以下的支持,可不考慮
// document.selection.createRange().text;
}