javascript - 无法将 setSelectionRange 添加到所选元素
问题描述
我使用“getElementById”选择了输入。在我想向该元素添加“setSelectionRange”方法之后。但是有一个错误“'htmlelement'类型上不存在属性'setselectionrange'”。可能是什么问题呢?
const textfield = document.getElementById("masktextfield")
console.log(textfield)
这就是控制台中显示的内容
现在添加“setselectionrange”
textfield.setSelectionRange(0,0) // property 'setselectionrange' does not exist on type 'htmlelement'
解决方案
const textfield = document.getElementById("masktextfield");
textfield.focus();
textfield.setSelectionRange(2, 5);
<input type="text" id="masktextfield" value="sathish"/>
推荐阅读
- python - Flask 静态文件内容类型是重定向后的 text/html
- material-ui - react-dom.development.js?61bb:530 警告:道具类名不匹配
- android - 运行 ./gradlew bundleRelease 不会在 react-native 中生成发布 aab
- sql - 如何在 sql 查询中进行采样以使用 pandas 获取数据帧
- html - 将登录按钮降低到我的网络应用程序的最底部
- keras - Keras LSTM 转换为 Core ML 导致不同形状的输入层
- python - 运行 Markovify 项目时出现 PyCharm 错误
- python - Selenium Webscraper Click 在 Morningstar 上不起作用
- react-native - 问题制作模型弹出平面列表的Press
- google-apps-script - 使用 Google Slides API 可以在同一批次更新期间在所有或任何剩余请求中使用一个请求的响应