document.selection window.getSelection()

IE9以下支持:document.selection    
IE9、Firefox、Safari、Chrome和Opera支持:window.getSelection()

屏幕取词

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

. 移除选中的内容

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

小实例http://www.jb51.net/article/2835.htm

http://www.jb51.net/article/23421.htm

<body> 
  <p> </p> 
  <p> 
    <textarea name="textfield" cols="50" rows="6">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字 
哪位老大能解决的呀?请多多帮忙!!!谢谢 
</textarea> 
  </p> 
  <p> 
    <input type="button" value="showSelection" onclick="alert(document.selection.createRange().text)"> 
    <input type="button" value="showclear" onclick="alert(document.selection.clear().text)"> 
    <input type="button" value="showtype" onclick="alert(document.selection.type)"> 
     
</p> 
  <p> 
    <textarea name="textfield" cols="50" rows="6" onselect="alert(document.selection.createRange().text)">就是现在文本域里有一段文字,当你选种其中几个字后点击一个按钮或者链接会弹出一个对话框,对话框的信息就是你选中的文字 
哪位老大能解决的呀?请多多帮忙!!!谢谢 
    </textarea> 
  </p> 

</body>
原文地址:https://www.cnblogs.com/qingqingzou-143/p/6702535.html