[Angular HTML] Implementing The Input Mask Cursor Navigation Functionality -- setSelectionRange
2024-08-28 05:41:06
@HostListener('keydown', ['$event', '$event.keyCode'])
onKeyDown($event: KeyboardEvent, keyCode) { if(keyCode !== TAB) {
$event.preventDefault();
} // get value for the key
const val = String.fromCharCode(keyCode);
// get position
const cursorPos = this.input.selectionStart; switch(keyCode) {
case LEFT_ARROW:
this.handleLeftArrow(cursorPos);
return;
case RIGHT_ARROW:
this.handleRightArrow(cursorPos);
return;
} overWriteCharAtPosition(this.input, val, cursorPos);
this.handleRightArrow(cursorPos);
} handleRightArrow(cursorPos) {
const valueBeforeCursor = this.input.value.slice(cursorPos + );
const nextPos = findIndex(valueBeforeCursor, (char) => !includes(SPECIAL_CHARACTERS, char));
if(nextPos > -) {
const newNextPos = cursorPos + nextPos + ;
this.input.setSelectionRange(newNextPos, newNextPos);
}
} handleLeftArrow(cursorPos) {
const valueAfterCursor = this.input.value.slice(, cursorPos);
const previousPos = findLastIndex(valueAfterCursor, (char) => !includes(SPECIAL_CHARACTERS, char));
if(previousPos > -) {
this.input.setSelectionRange(previousPos, previousPos);
}
}
We can use 'setSelectionRange(start, end)' to set cursor postion, in which start postion = end position.
最新文章
- linux压缩和解压缩命令大全
- php学习手记
- 左边图标右边文字,在div里居中
- 测试卡尔曼滤波器(Kalman Filter)
- appdata文件夹有什么用途?C盘appdata可以删除吗?
- 以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
- 在ASP.NET项目中使用CKEditor
- 【phpcms-v9】如何实现在含有子栏目的栏目下添加内容?
- Java 六种异常处理的陋习(转)
- Ajax (AppServ服务器练习)
- mac查看git路径
- PHP跨域jsonp方式
- 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)
- pycharm的常用快捷键
- html样式表格
- Java 简明教程
- 《Blue_Flke》团队项目软件系统设计改进
- 正则表达式,清除HTML标签,但要保留 <;br>;和<;img>;标签,其他的清除
- scrum站立会议介绍
- HDU 5699 二分+线性约束
热门文章
- 基于x86平台的Solaris安装视频(时长25分钟)
- Dubbo简易学习
- c#程序打包、机器代码生成(Ngen.exe)
- 【Uva 10163】Storage Keepers
- ONVIF客户端搜索设备获取rtsp地址开发笔记(精华篇)
- stm8开发环境配置及测试
- Maven中央仓库信息速查
- Manning.EJB.3.in.Action.2nd.Edition
- golang sync.Mutex(2)
- BZOJ2119: 股市的预测(后缀数组)