//脚本获取网页中选中文字
var word = document.selection.createRange().text; 
//获取选中文字所在的句子
var range =  document.selection.createRange();
range.expand("sentence");
var sentence = range.text;
 
alert(word);//弹出选中的文字
alert(sentence );
 
 

例如页面上有一个文本输入框:

<input type="text" id="txt" value="abcdefghijklmn"/>

则,设置该文本框中的最后三个字符光标选中:

var o = document.getElementById("txt");

var len = o.value.length;

o.focus();          //先使光标聚集在输入框中

o.setSelectionRange(len-3,len);

//获取光标起始位置

if (textarea.setSelectionRange) {
             pos = textarea.selectionStart;
         }

function locatePoint(){ 
var aCtrl = document.getElementById("txtContent"); 
if (aCtrl.setSelectionRange) { 
setTimeout(function() { 
aCtrl.setSelectionRange(0, 0); //将光标定位在textarea的开头,需要定位到其他位置的请自行修改 
aCtrl.focus(); 
}, 0); 
}else if (aCtrl.createTextRange) { 
var textArea=document.getElementById("txtContent"); 
var tempText=textArea.createTextRange(); 
tempText.moveEnd("character",0-tempText.text.length); 
tempText.select(); 

}

 
 

最新文章

  1. jQuery静态方法globalEval使用和源码分析
  2. Xenia and Weights(深度优先搜索)
  3. 【Mysql】日期时间格式化
  4. Android 调用系统的分享[完美实现同一时候分享图片和文字]
  5. Ztree使用
  6. Effective Java从零开始 - 就是爱Java
  7. DataGridView 去掉多余的列
  8. atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
  9. BadUSB的防范研究
  10. cocos2d-x2.0 win7第一次创建项目需要调用到的脚本(不断更新维护)//cocos2d-x 教程一
  11. sql servse 查询当前库内表索引值
  12. url中文参数乱码问题
  13. 利用世界杯,读懂 Python 装饰器
  14. Bootstraptable源码
  15. C# StreamReader.ReadLine统计行数的问题
  16. angular --- s3core移动端项目
  17. leetcode238
  18. 洛谷P1638逛画展
  19. Flex4学习笔记1---基本语法
  20. mint 设置无线 AP

热门文章

  1. 总结day23 ---- 网络编程,以及计算机基础概念
  2. Laravel5.5 使用第三方Vendor添加注册验证码
  3. 栈的理解和代码实现(java)
  4. laravel框架图片上传
  5. MDX常用几种查询对比
  6. IntelliJ IDEA 2018.3(Ultimate Edition)激活方法
  7. 必须声明表变量 &quot;@P0&quot;
  8. npm install 报错: WARN checkPermissions Missing write access to 解决方案
  9. 如何在本地连接服务器上的MySQL
  10. 【实战】Apache Shiro 1.2.4 RCE