码迷,mamicode.com
首页 > Windows程序 > 详细

document.selection window.getSelection()

时间:2017-04-13 12:33:11      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:code   一段   remove   dev   logs   create   .text   var   class   

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>

 

document.selection window.getSelection()

标签:code   一段   remove   dev   logs   create   .text   var   class   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!