参见英文答案 >
Select the text inside an input using Typescript in Angular 2 1个
如何在TypeScript中的输入字段中选择文本.
如何在TypeScript中的输入字段中选择文本.
在Javascript中这个wpuld看起来像这样:
document.getElementById('id').select();
但TypeScript不支持select(错误:[ts]属性’select’在类型’HTMLElement’上不存在.)
添加了一些想法:
它可能是一个问题beacouse我使用dx控件,他们创建一个包装器.你可以在这里看到html:
<dx-text-box _ngcontent-c1="" id="test" ng-reflect-disabled="false" ng-reflect-value="0:00" class="dx-texteditor dx-widget dx-textbox"> <div class="dx-texteditor-container"> <input autocomplete="off" class="dx-texteditor-input" type="text" spellcheck="false" tabindex="0" role="textbox"> <div data-dx_placeholder="" class="dx-placeholder dx-state-invisible"></div><div class="dx-texteditor-buttons-container"> </div></div></dx-text-box>
解决方法
document.getElementById(string)返回一个未定义select()方法的HTMLElement. HTMLInputElement定义一个select()方法并扩展HTMLElement,所以如果你转换为HTMLInputElement,它应该工作:
(document.getElementById('id') as HTMLInputElement).select();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。