先看动图如下,我们就可以很清楚的知道获取input标签中光标的索引的意思了。



由于IE支持document.selection,Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<script>
function getTxt1CursorPosition(){
var oTxt1 = document.getElementById("txt1");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE浏览器
cursurPosition= oTxt1.selectionStart;
}else{//IE
var range = document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}
</script>
<input type="text" id="txt1" value="abcde" onclick="getTxt1CursorPosition()"> </body>
</html>

本文转载自:https://www.cnblogs.com/xiaoyang002/p/4055716.html

最新文章

  1. 转: JAVA递归算法实例小结
  2. SqlBulkCopy
  3. strtr和str_replace字符替换函数
  4. Tomcat指定特定JDK版本
  5. Android自动化测试 - 自动化测试工具比较
  6. Annotation介绍@interface
  7. Linux企业级项目实践之网络爬虫(28)——爬虫socket处理
  8. this class is not key value coding-compliant for the key detailItem
  9. AxonVR:体验有触觉有温度的VR世界
  10. listview前几个item的图片怎么是空白的、listview更新了ui不起作用、在handler里更新了UI不起作用
  11. api接口json串换行
  12. Leetcode_217_Contains Duplicate
  13. 企业IT管理员IE11升级指南【8】—— Win7 IE8和Win7 IE11对比
  14. Water Buying
  15. vc调试不能入断点
  16. 目标检测(六)YOLOv2__YOLO9000: Better, Faster, Stronger
  17. .net获取程序根目录
  18. python虚拟环境 -- virtualenv , virtualenvwrapper
  19. Deepin Linux修改Grub引导
  20. 【Leetcode】109. Convert Sorted List to Binary Search Tree

热门文章

  1. ansible的playbook简单使用
  2. docker 列出每个容器的IP
  3. delphi怎样在关闭程序时弹出窗口?
  4. SQL约束(主键约束、外键约束、自动递增、不允许空值、值唯一、值默认、值限制范围)
  5. JAVA web 使用有盟推送总结
  6. 基于jQuery-ui实现多滑块slider
  7. BZOJ5475 WC2019数树(prufer+容斥原理+树形dp+多项式exp)
  8. python中切片的理解
  9. Ionic开发遇到的坑整理
  10. Awesome-VR