<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input class="input_dom" type="text" value="123456789669898">
<!-- <textarea name="11" class="input_dom" cols="30" rows="10">123456789669898</textarea> -->
<script>
var input_dom = document.querySelector('.input_dom');
//input_dom.focus();//获取焦点
//input_dom.blur();//失去焦点
setCursorPosition(input_dom, 8);//设置光标位置
console.log(getCursorPosistion(input_dom));//获取光标位置,输出:8
setSelectText(input_dom,2,6);//设置选中的内容
console.log(getSelectText());//输出选中的内容:3456
setCursorPosition(input_dom, 5);//设置光标位置
insertAfterText(input_dom,'我们很好');//在光标的位置插入内容
// 设置input 或 textarea 光标位置
function setCursorPosition(inputDom, position = 0) {
if (!inputDom) return console.error('dom元素不为null!');
if (!inputDom.setSelectionRange) return console.error('dom为input元素才有setSelectionRange函数!');
if (!document.selection) {
// 非IE浏览器
inputDom.focus();
inputDom.setSelectionRange(position, position);
} else {
// IE浏览器
var range = inputDom.createTextRange();
range.moveEnd("character", position);
range.moveStart("character", position);
range.select();
}
}
// 获取input 或 textarea 光标位置
function getCursorPosistion(inputDom) {
if (!document.selection) {
// 非IE浏览器
const start = inputDom.selectionStart;
const end = inputDom.selectionEnd;
return start === end ? start : [start, end];
} else {
// IE浏览器
inputDom.focus();
var selectRange = document.selection.createRange();
selectRange.moveStart("character", -inputDom.value.length);
return selectRange.text.length;
}
}
// 获取input输入框选中文字
function getSelectText() {
if(document.selection){
//IE浏览器获取选中文本
return document.selection.createRange().text
}else{
//非IE浏览器获取选中文本
return document.getSelection().toString();
}
}
/**
* 选中特定范围的文本
* 参数:
* inputDom [JavaScript DOM String] 当前对象
* startPosition [Int] 起始位置
* endPosition [Int] 终点位置
*/
function setSelectText(inputDom, startPosition, endPosition) {
var startPosition = parseInt(startPosition),
endPosition = parseInt(endPosition),
textLength = inputDom.value.length;
if (textLength) {
if (!startPosition) {
startPosition = 0;
}
if (!endPosition) {
endPosition = textLength;
}
if (startPosition > textLength) {
startPosition = textLength;
}
if (endPosition > textLength) {
endPosition = textLength;
}
if (startPosition < 0) {
startPosition = textLength + startPosition;
}
if (endPosition < 0) {
endPosition = textLength + endPosition;
}
if (inputDom.createTextRange) {
// IE Support
var range = inputDom.createTextRange();
range.moveStart("character", -textLength);
range.moveEnd("character", -textLength);
range.moveStart("character", startPosition);
range.moveEnd("character", endPosition);
range.select();
} else {
// Firefox support
inputDom.setSelectionRange(startPosition, endPosition);
inputDom.focus();
}
}
}
/**
* 在光标后插入文本
* 参数:
* inputDom [JavaScript DOM String] 当前对象
* value [String] 要插入的文本
*/
function insertAfterText(inputDom, value) {
var selectRange;
if (document.selection) {
// IE Support
inputDom.focus();
selectRange = document.selection.createRange();
selectRange.text = value;
inputDom.focus();
} else if (inputDom.selectionStart || inputDom.selectionStart == '0') {
// Firefox support
var startPosition = inputDom.selectionStart;
var endPosition = inputDom.selectionEnd;
var scrollTop = inputDom.scrollTop;
inputDom.value = inputDom.value.substring(0, startPosition) + value + inputDom.value.substring(endPosition, inputDom.value.length);
inputDom.focus();
inputDom.selectionStart = startPosition + value.length;
inputDom.selectionEnd = startPosition + value.length;
inputDom.scrollTop = scrollTop;
}
else {
inputDom.value += value;
inputDom.focus();
}
}
</script>
</body>
</html>
javascript获取input/textarea以及设置光标位置
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 第一步:监听输入框的鼠标失焦事件@blur 第二步: 获取失去交点时的光标在输入内容中的位置,data里定义一个变...
- 原文链接:vue 如何获取input中光标位置,并且点击按钮在当前光标后追加内容[https://blog.csd...
- 一、React不是一个MVVM的框架,VUE属于MVVM框架; 二、约束性组件和非约束性组件 2.1非约束性组件 ...