gpt4 book ai didi

javascript - 正确输入 nextElementSibling 以便可以使用 .focus() 吗?

转载 作者:行者123 更新时间:2023-12-03 00:18:39 25 4
gpt4 key购买 nike

我正在尝试使用 nextElementSibling 以编程方式移动表单中的焦点,但是我也在使用 Typescript 并想要输入我的变量/常量...

我已经成功无需打字,并使用以下内容:

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();
}
}

然而,在谷歌搜索后,我发现 nextElementSibling 的类型实际上应该是 Element,根据 Mozilla

有人可以帮忙吗?

更新:

看来 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();
}
}

关于javascript - 正确输入 nextElementSibling 以便可以使用 .focus() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54424080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com