[英]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.