document.selection

一、document.selection 介绍

  document.selection 表示当前网页中的选中内容。代表了当前激活选中区,即高亮文本块,和/或文档中用户可执行某些操作的其它元素。  

  selection对象非w3c标准,部分浏览器支持。典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户。

    ① IE:document.selection

    ② FireFox:window.getSelection()

    ③ document.selection只有IE支持,window.getSelection()也只有 FireFox 和 Safari 支持,都不是标准语法。

   用户和脚本都可以创建选中区。

    用户创建选中区的办法是拖曳文档的一部分。

    脚本创建选中区的办法是在文本区域或类似对象上调用 select 方法。要获取当前选中区,请对 document 对象应用 selection 关键字。要对选中区执行操作,请先用 createRange 方法,从选中区创建一个文本区域对象。

  方法有:

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

  属性有:

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

二、document.selection.createRange()

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

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

三、示例

  限定 input 的字数:

jQuery.fn.maxLength = function(max){
    this.each(function(){
        var type = this.tagName.toLowerCase();
        var inputType = this.type? this.type.toLowerCase() : null;
        if(type == "input" && inputType == "text" || inputType == "password"){
            //应用标准的maxLength
            this.maxLength = max;
        }else if(type == "textarea"){
            this.onkeypress = function(e){
                var ob = e || event;
                var keyCode = ob.keyCode;
                var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd;
                return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection);
            };
            this.onkeyup = function(){
                if(this.value.length > max){
                    this.value = this.value.substring(0,max);
                }
            };
        }
    });
};
时间: 2024-07-30 07:58:22

document.selection的相关文章

IE下iframe中使用滤镜document.selection.createRange().text获取不到值得解决办法

通常的写法是select方法后跟一个blur方法,但是这里我们不能使用blur方法,应该将blur改为window.parent.document.body.focus(); IE下iframe中使用滤镜document.selection.createRange().text获取不到值得解决办法,布布扣,bubuko.com

DHTML之-----document.selection 的 createRange

一.document.selection 介绍 document.selection 表示当前网页中的选中内容. 方法有: clear 清除选中的内容 empty 取消选中 createRange 返回 TextRange 或 ControlRange 对象 createRangeCollection 不支持 属性有: type 选中内容的类型 typeDetail 不支持 二.document.selection.createRange()详细介绍 document.selection.cre

window.getSelection和document.selection getSelection

window.getSelection和document.selection 返回一个 Selection 对象,表示用户选择的文本. selection 是一个 Selection 对象. 如果想要将 selection 转换为字符串,可通过连接一个空字符串("")或使用 String.toString() 方法. IE9以下支持:document.selection  IE9.Firefox.Safari.Chrome和Opera支持:window.getSelection() a

关于document.selection和TextRange对象的介绍

document.selection只有IE支持 window.getSelection()也只有FireFox和Safari支持,都不是标准语法. selection 对象代表了当前激活选中区,即高亮文本块,或文档中用户可执行某些操作的其它元素.selection 对象的典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户. 用户和脚本都可以创建选中区.用户创建选中区的办法是拖曳文档的一部分.脚本创建选中区的办法是在文本区域或类似对象上调用 sele

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("c

document.selection.createRange方法----获取用户选择文本

document.selection.createRange方法----获取用户选择文本:http://www.cnblogs.com/yxyht/archive/2013/01/23/2872605.html

document.selection和window.getSelection属性和方法详解

IE系列函数:TextRange的常用属性与方法: 属性:boundingHeight 获取绑定TextRange对象的矩形的高度boundingLeft 获取绑定TextRange 对象的矩形左边缘和包含TextRange对象的左侧之间的距离offsetLeft 获取对象相对于版面或由offsetParent属性指定的父坐标的计算左侧位置offsetTop 获取对象相对于版面或由offsetParent属性指定的父坐标的计算顶端位置htmlText 获取绑定TextRange对象的矩形的宽度t

学习总结之javaScript document对象详解

Document对象内容集合 document 文挡对象 - JavaScript脚本语言描述———————————————————————注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写否则会提示你一个错误信息 “引用的元素为空或者不是对象\\\\\”——————————————————————— 对象属性document.title //设置文档标题等价于HTML的title标签document.bgColor //设置页面背景色document.fgColor

JS的Document属性和方法小结

Document想必大家并不陌生吧,在使用js的过程中会经常遇到它,那么它有哪些属性.哪些方法,在本文将以示例为大家详细介绍下,希望对大家有所帮助 document.title //设置文档标题等价于HTML的title标签 document.bgColor //设置页面背景色 document.fgColor //设置前景色(文本颜色) document.linkColor //未点击过的链接颜色 document.alinkColor //激活链接(焦点在此链接上)的颜色 document.