Querying Selected Text
查询选择的文本
引用
This is an area where little standardization exists, but it is possible to query the selected text in all modern browsers.
在这个方面,没有什么标准存在;但还是有可能读取用户选择的文本的:
function getSelectedText() {
if (window.getSelection) {
// This technique is the most likely to be standardized.
// getSelection() returns a Selection object, which we do not document.
return window.getSelection().toString();
}
else if (document.getSelection) {
// This is an older, simpler technique that returns a string
return document.getSelection();
}
else if (document.selection) {
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
return document.selection.createRange().text;
}
}
The Selection and TextRange objects
Selection和TextRange对象
引用
The getSelection() methods of the Window and Document object do not return selected text if it is within an <input> or <textarea> form element: they only return text selected from the body of the document itself.
The IE document.selection property, on the other hand, returns selected text from anywhere in the document.
getSelection()方法是无法返回输入框或文本域中的内容的,它只能返回文档本身的文字内容。
但是IE独有的document.selection属性倒是哪里的内容都可以访问到。
function getTextFieldSelection(e) {
if (e.selectionStart != undefined && e.selectionEnd != undefined) {
var start = e.selectionStart;
var end = e.selectionEnd;
return e.value.substring(start, end);
}
else return ""; // Not supported on this browser
}
很多网上划词的功能都是建立在这些方法上的,值得学习~
分享到:
相关推荐
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于...它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络、存储和图形等,但这些都可以由它的宿主环境提供支持。
它提供语法来操控文本、数组、日期以及正则表达式等,不支持I/O,比如网络、存储和图形等,但这些都可以由它的宿主环境提供支持。它已经由ECMA(欧洲电脑制造商协会)通过ECMAScript实现语言的标准化。它被世界上的...
它提供语法来操控文本、数组、日期以及正则表达式等,不支持I/O,比如网络、存储和图形等,但这些都可以由它的宿主环境提供支持。它已经由ECMA(欧洲计算机制造商协会)通过ECMAScript实现语言的标准化[5]。它被世界...
它提供语法来操控文本、数组、日期以及正则表达式,不支持I/O(比如网络、存储和图形等),但可以由它的宿主环境提供支持。它已经由ECMA(欧洲计算机制造商协会)通过ECMAScript实现语言的标准化。它被世界上的绝大...
第十章 DOM DOM是针对XML和HTML文档的一个API:即规定了实现文本节点操控的属性、方法,具体实现由各自浏览器实现。 1. 节点层次 1) 文档节点:document,每个文档的根节点。 2) 文档元素:即<html>元素,文档...
6.3.3 常见的“操控DOM”方法 6.3.4 常见的“尺寸大小/定位”方法 6.3.5 常见的“特效动画”方法 6.3.6 DomHelper简介 6.4 DomQuery入门 6.4.1 元素选择符Selector 6.4.2 属性选择符Attributes Selectors ...
6.3.3 常见的“操控DOM”方法 6.3.4 常见的“尺寸大小/定位”方法 6.3.5 常见的“特效动画”方法 6.3.6 DomHelper简介 6.4 DomQuery入门 6.4.1 元素选择符Selector 6.4.2 属性选择符Attributes Selectors ...
这一版本在HTML5技术上带来了相当大的改进,Cross-Origin Resource Sharing (CORS)带来了XMLHttpRequest跨域名安全运用,File API Writer允许直接访问二进制数据,对JavaScript的数组类型存储和操控也更为高效,...
//这个是使用JavaScript的方法,这个没有问题。 //下面是使用jquery的方法操控canvas。 $(document).ready(function(){ var cas=$('#canvas').getContext('2d');<span xss=removed> </span>//出现问题,对象获取...
Chrome Frame 会把最新版的Chrome Webkit 内核和JavaScript 引擎注入到IE中, IE浏览器将获得Chrome的性能和功能 目录 摘要 I ABSTRACT II 专业名词清单 III 第一章 绪论 1 1.1 研究背景与意义 1 1.2国内外相关...