【问题标题】:Typescript disable all elements within specific ID that has a class打字稿禁用具有类的特定 ID 中的所有元素
【发布时间】:2017-12-22 05:31:06
【问题描述】:

我有一个函数可以切换 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 属性?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    你需要告诉 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
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      相关资源
      最近更新 更多