document.selection

一、document.selection 介绍 

document.selection 表示当前网页中的选中内容。 

方法有: 

    clear 清除选中的内容 
    empty 取消选中 
    createRange 返回 TextRange 或 ControlRange 对象 
    createRangeCollection 不支持 

属性有: 



    type 选中内容的类型 
    typeDetail 不支持 



二、document.selection.createRange()详细介绍 

     document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回ControlRange 对象。 

配合 execCommand,在 HTML 编辑器中很有用,比如:文字加粗、斜体、复制、粘贴、创建超链接等。 



例子: 
Html代码  收藏代码    
[html]  view plain  copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
  2.     <html xmlns="http://www.w3.org/1999/xhtml" >    
  3.     <head>    
  4.         <title>document.selection 的 createRange</title>    
  5.     </head>    
  6.     <body>    
  7.         
  8.       <div>请选中这里的部分文字。</div>    
  9.       <div><input type="button" value="加粗" onclick="javascript:Bold();" /></div>    
  10.     <script type="text/javascript" language="javascript">    
  11.     <!--    
  12.     function Bold()    
  13.     {    
  14.         var r = document.selection.createRange();    
  15.         r.execCommand("Bold");    
  16.     }    
  17.     -->    
  18.     </script>    
  19.         
  20.     </body>    
  21.     </html>    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ahstunwy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值