简体   繁体   English

Typescript禁用特定ID中具有类的所有元素

[英]Typescript disable all elements within specific ID that has a class

I have a function that is toggling the enabled / disabled statuses of input fields within a div. 我有一个功能,用于切换div中输入字段的启用/禁用状态。 This div has a unique ID and the div contains inputs that have the same class name. 该div具有唯一的ID,并且div包含具有相同类名的输入。

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

When I try this, typescript is telling me that [ts] Property 'disabled' does not exist on type 'Element'. 当我尝试此操作时,打字稿告诉我[ts] Property 'disabled' does not exist on type 'Element'.

Do I need to cast this element in some way to be able to access the disabled property? 我是否需要以某种方式强制转换此元素才能访问Disabled属性?

You need to tell TypeScript that this is an input 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 隐藏具有特定类的所有元素并按ID显示 - Hide all elements with a specific class and show by ID 我如何选择全部 <th> 具有特定ID的表中“sortasc”类的元素? - How do I select all <th> elements of class “sortasc” within a table with a specific id? 如何扫描类中的所有元素以查找特定的ID,并为不同的ID重复该过程? - How do I scan all elements within a class to find a specific id and repeat the process for different ids? 在给定id的元素内选择类的所有元素 - selecting all elements of a class within elements of given id 从节点内访问具有 ID 的多个元素,但也具有特定的 class - Access multiple elements with ID from within a node, but also with specific class 隐藏类中与变量 ID 不匹配的所有元素 - Hide all elements within a class that don't match the ID of a variable 选择具有某个类的所有元素以及具有特定ID的父元素 - Selecting all elements with some class and parent with a specific id 如何使用 document.querySelector() 查找具有特定 ID 的 div 中具有特定名称 class 的元素 - How to use document.querySelector() to find elements with a specific class name that are within a div with a specific ID 赛普拉斯在元素列表中查找特定 ID - Cypress Find specific ID within list of elements 如何在 Beautiful Soup 和 Selenium 中查找特定 div ID 中的所有元素 - How to find all elements within specific div ID in Beautiful Soup and Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM