我有一个动态更新 HTML aria-expanded
的函数属性为真或假。但是,当我输入 element
作为HTMLElement
, 我收到了 Argument of type 'boolean' is not assignable to parameter of type 'string'
expandEotyDocumentsPanel(element: HTMLElement) {
this.eotyExpanded = !this.eotyExpanded;
element.setAttribute('aria-expanded', this.eotyExpanded);
}
您可能已经注意到,
this.eotyExpanded
是一个 bool 值。关于
setAttribute()
的第二个参数,MDN 上的文档说:A DOMString containing the value to assign to the attribute. Any non-string value specified is converted automatically into a string.
所以我认为提供一个 bool 值就可以了。
我怎样才能抑制这个错误?
谢谢。
最佳答案
元素的属性不能是 bool 值,所以你可能只是把它变成字符串而不是new Boolean(this.eotyExpanded).toString()
关于javascript - 如何解决 setAttribute() 函数中 'boolean' 类型的参数不可分配给 'string' 类型的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57607524/