NodeList项没有focus方法。然而,我看过一些用字面上的nodeList[index].focus()写的文章,这肯定是不正确的,对吧?
我们如何聚焦NodeList中的元素呢?
let nodeList:NodeList = el.nativeElement.querySelectorAll('a');
...
nodeList[0].focus(); // Property 'focus' does not exist on type 'Node'
(nodeList[0] as HTMLElement).focus(); // doesn't work发布于 2021-06-27 08:35:24
NodeList不是一个足够窄的类型;您必须指定它是HTMLElements的节点列表。您可以使用NodeListOf<HTMLElement>类型来完成此操作:
let nodeList: NodeListOf<HTMLElement> = el.nativeElement.querySelectorAll('a');
nodeList[0].focus();请注意,您也可以让编译器推断出正确的nodeList类型,而不必显式地键入它:
let nodeList = el.nativeElement.querySelectorAll('a');
nodeList[0].focus();https://stackoverflow.com/questions/68147147
复制相似问题