document.selection window.getSelection()

时间:2017-04-13 12:33:11   收藏:0   阅读:324

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>

 

原文:http://www.cnblogs.com/qingqingzou-143/p/6702535.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!