[英]Typescript disable all elements within specific ID that has a class
我有一个功能,用于切换div中输入字段的启用/禁用状态。 该div具有唯一的ID,并且div包含具有相同类名的输入。
const el = document.getElementById('slice_' + slice).getElementsByClassName('shiftSlice');
for (let i = 0; i < el.length; i++) {
el[i].disabled = true;
}
当我尝试此操作时,打字稿告诉我[ts] Property 'disabled' does not exist on type 'Element'.
我是否需要以某种方式强制转换此元素才能访问Disabled属性?
您需要告诉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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.