<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试光标停留在文字后面</title>
</head>
<body>
<input type="text" id="cursorPos" value="光标永远停留在文字最后">
<script type="text/javascript">
window.onload=function(){
var cursorPos = document.getElementById("cursorPos");
cursorPos.onclick = cursorPos.onkeyup = function () {
var len = this.value.length;
if (this.setSelectionRange) {
this.setSelectionRange(len,len);
} else {
var a = this.createTextRange();
a.moveStart('character', len);
a.collapse(true);
a.select();
}
}
}
</script>
</body>
</html>
js 光标停留在文字后面 《超实用的JavaScript代码段》笔记
最新推荐文章于 2022-08-27 18:06:56 发布