正确键入 nextElementSibling,以便可以使用 .focus()?
Correct typing for nextElementSibling so .focus() can be used?
我正在尝试使用 nextElementSibling
以编程方式在我的表单中移动焦点,但是我也在使用 Typescript 并想输入我的 variables/constants...
我 没有输入 并使用以下命令就成功了:
myFunct(event) {
const myEl = event.srcElement;
const nextElement = myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}
但是,当在代码上使用类型时,我遇到了不一致和以下 IntelliJ 警告:
Property 'focus' does not exist on type 'Element'
myFunct(event: KeyboardEvent) {
const myEl: Element = event.srcElement;
const nextElement: Element = myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}
然而,根据 Mozilla[=19=,我发现 nextElementSibling
的类型实际上应该是 Element
]
有人可以帮忙吗?
更新:
看来 event
上的 KeyboardEvent
实际上是导致问题的原因..
From MDN: Element inherits properties from its parent, Element, and implements those from GlobalEventHandlers and TouchEventHandlers.
因此,您可以将代码更改为(使用 typescriptlang.org 测试的代码):
function myFunct(event: KeyboardEvent) {
const myEl = <HTMLElement>event.srcElement;
const nextElement = <HTMLElement>myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}
我正在尝试使用 nextElementSibling
以编程方式在我的表单中移动焦点,但是我也在使用 Typescript 并想输入我的 variables/constants...
我 没有输入 并使用以下命令就成功了:
myFunct(event) {
const myEl = event.srcElement;
const nextElement = myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}
但是,当在代码上使用类型时,我遇到了不一致和以下 IntelliJ 警告:
Property 'focus' does not exist on type 'Element'
myFunct(event: KeyboardEvent) {
const myEl: Element = event.srcElement;
const nextElement: Element = myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}
然而,根据 Mozilla[=19=,我发现 nextElementSibling
的类型实际上应该是 Element
]
有人可以帮忙吗?
更新:
看来 event
上的 KeyboardEvent
实际上是导致问题的原因..
From MDN: Element inherits properties from its parent, Element, and implements those from GlobalEventHandlers and TouchEventHandlers.
因此,您可以将代码更改为(使用 typescriptlang.org 测试的代码):
function myFunct(event: KeyboardEvent) {
const myEl = <HTMLElement>event.srcElement;
const nextElement = <HTMLElement>myEl.nextElementSibling;
if (nextElement) {
nextElement.focus();
}
}