有趣的js获取input标签中光标的索引
2024-08-30 13:13:30
先看动图如下,我们就可以很清楚的知道获取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
最新文章
- 转: JAVA递归算法实例小结
- SqlBulkCopy
- strtr和str_replace字符替换函数
- Tomcat指定特定JDK版本
- Android自动化测试 - 自动化测试工具比较
- Annotation介绍@interface
- Linux企业级项目实践之网络爬虫(28)——爬虫socket处理
- this class is not key value coding-compliant for the key detailItem
- AxonVR:体验有触觉有温度的VR世界
- listview前几个item的图片怎么是空白的、listview更新了ui不起作用、在handler里更新了UI不起作用
- api接口json串换行
- Leetcode_217_Contains Duplicate
- 企业IT管理员IE11升级指南【8】—— Win7 IE8和Win7 IE11对比
- Water Buying
- vc调试不能入断点
- 目标检测(六)YOLOv2__YOLO9000: Better, Faster, Stronger
- .net获取程序根目录
- python虚拟环境 -- virtualenv , virtualenvwrapper
- Deepin Linux修改Grub引导
- 【Leetcode】109. Convert Sorted List to Binary Search Tree