获取鼠标选中文字

原博地址:http://www.cnblogs.com/NetSos/archive/2011/02/14/1954345.html

本文关键点:

document.selection.createRange().text //ie

document.getSelection();//ff

小插曲:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
selection   对象
 
代表了当前激活选中区,即高亮文本块,文档中用户可执行某些操作的其它元素。
 
selection   对象的典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户。
   
用户和脚本都可以创建选中区。用户创建选中区的办法是拖曳文档的一部分。
 
脚本创建选中区的办法是在文本区域或类似对象上调用   select    方法。
 
要获取当前选中区,请对   document   对象应用   selection   关键字。
 
要对选中区执行操作,请先用   createRange   方法从选中区创建一个文本区域对象。
   
一个文档同一时间只能有一个选中区。选中区的类型决定了其中为空或者包含文本和/或元素块。尽管空的选中区不包含任何内容,你仍然
 
可以用它作为文档中的位置标志。

下面的这个demo就是关于,如何获取鼠标选中的值:

扫描二维码关注公众号,回复: 2165605 查看本文章
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
     <head></head>
     <body>
         分享到新浪微博功能实现之截图 是不小贺啊小贺
         http: //netsos.cnblogs.com/
         分享到新浪微博啊分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博分享到新浪微博
     <script>
         var  funcGetSelectText = function(){
             var  txt = '' ;
             if (document.selection){
                 txt = document.selection.createRange().text; //ie
             } else {
                 txt = document.getSelection();
             }
             return  txt.toString();
         }
         var  container = container || document;
         container.onmouseup = function(){
             var  txt = funcGetSelectText();
             if (txt)
             {
                 alert(txt);
             }
         }
     </script>
     </body>
</html>


猜你喜欢

转载自blog.csdn.net/qq_40693828/article/details/81015719