【问题标题】:"Error is Property 'focus' does not exist on type 'EventTarget'.ts" : focus is not bale to work in document,querySelector“错误是 'EventTarget'.ts 类型上不存在属性'焦点'”:焦点不能在文档中工作,querySelector
【发布时间】:2021-04-01 17:19:19
【问题描述】:

为什么焦点会出错?错误是“EventTarget”类型上不存在属性“焦点”。ts

请任何人帮助我。

@HostListener('keydown.tab', [ '$event'])
 keyfunction(event: KeyboardEvent): void {
   // First element: always first element is h2 where tabindex avaliable.
   let focusable = document.querySelector('.modal').querySelectorAll('[tabindex],button');
   if (focusable.length) {
       const first = focusable[0];
       const last = focusable[focusable.length - 1];
       const shift = event.shiftKey;
    
       if (shift) {
         if (event.target === first) {
           last.focus();// **Error is: Error is Property 'focus' does not exist on type **
           
           event.preventDefault();
         }
     } else {
         if (event.target === last) {
            first.focus();// Getting error is here
           event.preventDefault();
         }
       }
     }

【问题讨论】:

  • 请告诉我有什么问题?

标签: javascript angularjs typescript


【解决方案1】:

lastfirst 默认具有 Element 类型,因为 querySelectorAll 返回类型为 NodeListOf<Element> 的值。

我的建议是在声明时更改类型。

const first: HTMLElement = focusable[0] as HTMLElement;
const last: HTMLElement = focusable[focusable.length - 1] as HTMLElement;

【讨论】:

    猜你喜欢
    • 2020-07-16
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 2016-09-14
    • 2021-07-04
    • 2021-07-02
    • 2019-11-11
    • 2019-08-16
    相关资源
    最近更新 更多