javascript - Typescript 禁用特定 ID 内具有类的所有元素

标签 javascript typescript

我有一个功能可以切换 div 内输入字段的启用/禁用状态。该 div 具有唯一的 ID,并且该 div 包含具有相同类名的输入。

const el = document.getElementById('slice_' + slice).getElementsByClassName('shiftSlice');
        for (let i = 0; i < el.length; i++) { 
            el[i].disabled = true;
        }

当我尝试此操作时, typescript 告诉我 [ts] 属性“disabled”在“Element”类型上不存在。

我是否需要以某种方式强制转换此元素才能访问禁用的属性?

最佳答案

您需要告诉 TypeScript 这是一个输入元素:

const el = document.getElementById('slice_' + slice).getElementsByClassName('shiftSlice');
for (let i = 0; i < el.length; i++) {
    (<HTMLInputElement>el[i]).disabled = true; // note the type assertion on the element
}

关于javascript - Typescript 禁用特定 ID 内具有类的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45148286/

相关文章:

javascript - 尝试将 BASH 安装到 VSCode 时无法编辑用户设置。 "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",

javascript - 如何使用现有的 typescript 命名空间类作为 Nodejs 的外部模块

javascript - 有什么方法可以使用 Typescript 转换或更改对象的定义吗?

angular - Karma-Jasmine:TypeError:无法读取未定义的属性(读取 'get')

javascript - 如何让 ASP.NET MVC 包管理器呈现 type = "module"属性的脚本标记?

javascript - html 输入 - 仅数字和覆盖模式

javascript - 如何创建 foreach 循环以将音节放置在各自单词的每个输入中

javascript - 我想在 .subscribe 内创建一个本地存储,但它在所有函数之后创建本地存储

javascript - 在 jquery 中检测数字是 INT 还是 FLOAT

angular - 'WeakMap' 的所有声明必须具有相同的类型参数