屏幕取词: window.getSelection和document.selection

IE9以下支持:document.selection   
IE9、Firefox、Safari、Chrome和Opera支持:window.getSelection()
应 用 :
1>. 屏幕取词

function getWord(){ 
     var word = window.getSelection?window.getSelection():document.selection.createRange().text; 
     alert( word ) ;
}
document.body.addEventListener("click", getWord, false);

2>. 移除选中的内容

function removeWord(){ 
    window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
document.body.addEventListener("click", removeWord, false);

你可能感兴趣的:(屏幕取词: window.getSelection和document.selection)