首页 > 其他分享 >ts中的dom元素和event事件类型声明

ts中的dom元素和event事件类型声明

时间:2024-04-18 18:12:02浏览次数:20  
标签:string dom MouseEvent ts Event HTMLElement 类型 input event

1, HTMLElement 和 Element

    <div id="divClick"></div>
     const docu = document.getElementById('divClick');
     const docu1 = document.querySelector('#divClick');

把鼠标分别放在docu和docu1上:


HTMLElement

HTMLElement 是 HTML 文档中某个元素的具体类型,该类型包含了所有 HTML 元素的共有属性和方法,如 CSSStyleDeclaration 和 EventTarget。

Element

Element 是 HTMLElement 的基本类型,它表示文档中任何类型的标记元素(如 div、span、p 等),包括非 HTML 元素。 Element 类型只包含最基本的属性(如 tagName 和 attributes)和方法(如 getAttribute() 和 setAttribute())。

HTMLElement 和 Element 两者区别

Element 是一个通用的基类,所有 Document 对象下的对象都继承自它。这个接口描述了所有相同种类的元素所普遍具有的方法和属性。一些接口继承自 Element 并且增加了一些额外功能的接口描述了具体的行为。例如, HTMLElement 接口是所有 HTML 元素的基本接口,而 SVGElement 接口是所有 SVG 元素的基础。大多数功能是在这个类的更深层级的接口中被进一步制定的。

HTMLElement是extends Element、然后额外增加一些额外的接口。
Element是所有元素的基类。

在node_modules下面的typescript/lib/lib.dom.d.ts:


interface HTMLElement extends Element, ElementCSSInlineStyle, ElementContentEditable, GlobalEventHandlers, HTMLOrSVGElement {
        accessKey: string;
        readonly accessKeyLabel: string;
        autocapitalize: string;
        dir: string;
        draggable: boolean;
        hidden: boolean;
        inert: boolean;
        innerText: string;
        lang: string;
        readonly offsetHeight: number;
        readonly offsetLeft: number;
        readonly offsetParent: Element | null;
        readonly offsetTop: number;
        readonly offsetWidth: number;
        outerText: string;
        spellcheck: boolean;
        title: string;
        translate: boolean;
        attachInternals(): ElementInternals;
        click(): void;
        addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => 
                         any, options?: boolean | AddEventListenerOptions): void;
        addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
        removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => 
                         any, options?: boolean | EventListenerOptions): void;
        removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;

2. 具体的dom元素类型声明

虽然说每次获取dom结点,都可以通过设置类型为HTMLElement。但是有的时候也会有问题, 比如

<input type="text" value="hello">
    
    const input = document.querySelector('input[type=text]') as HTMLElement;
    console.log(input!.value);   // 类型“HTMLElement”上不存在属性“value”

这时候,我们就需要把它设置为更具体一点的dom类型。

      const input = document.querySelector('input[type=text]') as HTMLInputElement;
       console.log(input!.value);

设置为HTMLInputElement,TS不报错原因
typescript/lib/lib.dom.d.ts:


    interface HTMLInputElement extends HTMLElement {
         ...
        /** Gets or sets a string containing a regular expression that the user's input must match. */
        pattern: string;
        /** Gets or sets a text string that is displayed in an input field as a hint or prompt to users as the format or type of information 
            they need to enter.The text appears in an input field until the user puts focus on the field. */
        placeholder: string;
        readOnly: boolean;
        /** When present, marks an element that can't be submitted without a value. */
        required: boolean;
        selectionDirection: "forward" | "backward" | "none" | null;
        /** Gets or sets the end position or offset of a text selection. */
        selectionEnd: number | null;
        /** Gets or sets the starting position or offset of a text selection. */
        selectionStart: number | null;
        size: number;
        /** The address or URL of the a media resource that is to be considered. */
        src: string;
        /** Defines an increment or jump between values that you want to allow the user to enter. When used with the max and min attributes,
             lets you control the range and increment (for example, allow only even numbers) that the user can enter into an input field. */
        step: string;
        /** Returns the content type of the object. */
        type: string;
         /** Returns the value of the data at the cursor's current position. */
        value: string;
        ...
}

HTMLInputElement是继承HTMLElement的,基于HTMLElement进行扩展的
这里是moudles ts文件中的一些继承HTMLElement的类型


    interface HTMLElementTagNameMap {
        "a": HTMLAnchorElement;
        "abbr": HTMLElement;
        "address": HTMLElement;
        "applet": HTMLAppletElement;
        "area": HTMLAreaElement;
        "article": HTMLElement;
        "aside": HTMLElement;
        "audio": HTMLAudioElement;
        "b": HTMLElement;
        "base": HTMLBaseElement;
        "bdi": HTMLElement;
        "bdo": HTMLElement;
        "blockquote": HTMLQuoteElement;
        "body": HTMLBodyElement; 
        "br": HTMLBRElement;
        "button": HTMLButtonElement;
        "canvas": HTMLCanvasElement;
        "caption": HTMLTableCaptionElement;
        "cite": HTMLElement;
        "code": HTMLElement;
        "col": HTMLTableColElement;
        "colgroup": HTMLTableColElement;
        "data": HTMLDataElement;
        "datalist": HTMLDataListElement;
        "dd": HTMLElement;
        "del": HTMLModElement;
        "details": HTMLDetailsElement;
        "dfn": HTMLElement;
        "dialog": HTMLDialogElement;
        "dir": HTMLDirectoryElement;
        "div": HTMLDivElement;
        "dl": HTMLDListElement;
        "dt": HTMLElement;
        "em": HTMLElement;
        "embed": HTMLEmbedElement;
        "fieldset": HTMLFieldSetElement;
        "figcaption": HTMLElement;
        "figure": HTMLElement;
        "font": HTMLFontElement;
        "footer": HTMLElement;
        "form": HTMLFormElement;
        "frame": HTMLFrameElement;
        "frameset": HTMLFrameSetElement;
        "h1": HTMLHeadingElement;
        "h2": HTMLHeadingElement;
        "h3": HTMLHeadingElement;
        "h4": HTMLHeadingElement;
        "h5": HTMLHeadingElement;
        "h6": HTMLHeadingElement;
        "head": HTMLHeadElement;
        "header": HTMLElement;
        "hgroup": HTMLElement;
        "hr": HTMLHRElement;
        "html": HTMLHtmlElement;
        "i": HTMLElement;
        "iframe": HTMLIFrameElement;
        "img": HTMLImageElement;
        "input": HTMLInputElement;
        "ins": HTMLModElement;
        "kbd": HTMLElement;
        "label": HTMLLabelElement;
        "legend": HTMLLegendElement;
        "li": HTMLLIElement;
        "link": HTMLLinkElement;
        "main": HTMLElement;
        "map": HTMLMapElement;
        "mark": HTMLElement;
        "marquee": HTMLMarqueeElement;
        "menu": HTMLMenuElement;
        "meta": HTMLMetaElement;
        "meter": HTMLMeterElement;
        "nav": HTMLElement;
        "noscript": HTMLElement;
        "object": HTMLObjectElement;
        "ol": HTMLOListElement;
        "optgroup": HTMLOptGroupElement;
        "option": HTMLOptionElement;
        "output": HTMLOutputElement;
        "p": HTMLParagraphElement;
        "param": HTMLParamElement;
        "picture": HTMLPictureElement;
        "pre": HTMLPreElement;
        "progress": HTMLProgressElement;
        "q": HTMLQuoteElement;
        "rp": HTMLElement;
        "rt": HTMLElement;
        "ruby": HTMLElement;
        "s": HTMLElement;
        "samp": HTMLElement;
        "script": HTMLScriptElement;
        "section": HTMLElement;
        "select": HTMLSelectElement;
        "slot": HTMLSlotElement;
        "small": HTMLElement;
        "source": HTMLSourceElement;
        "span": HTMLSpanElement;
        "strong": HTMLElement;
        "style": HTMLStyleElement;
        "sub": HTMLElement;
        "summary": HTMLElement;
        "sup": HTMLElement;
        "table": HTMLTableElement;
        "tbody": HTMLTableSectionElement;
        "td": HTMLTableDataCellElement;
        "template": HTMLTemplateElement;
        "textarea": HTMLTextAreaElement;
        "tfoot": HTMLTableSectionElement;
        "th": HTMLTableHeaderCellElement;
        "thead": HTMLTableSectionElement;
        "time": HTMLTimeElement;
        "title": HTMLTitleElement;
        "tr": HTMLTableRowElement;
        "track": HTMLTrackElement;
        "u": HTMLElement;
        "ul": HTMLUListElement;
        "var": HTMLElement;
        "video": HTMLVideoElement;
        "wbr": HTMLElement;
}

3. Event 事件类型

3.1 Event类型介绍

Event接口表示一个DOM事件,包含了事件的基本信息,如事件类型、目标元素等。可以使用该接口来定义事件的类型
可以理解为Event类型是事件的基本类型,所有不同类型的event事件都是继承于Event类型。

简单写一个继承。例如:


    <template>
        <div @click="clickMyEvent">MyEvent</div>
    </template>
    
    interface MyEvent extends Event {
        mess: string
    }

    const clickMyEvent = (e: MouseEvent) => {
        // 这里的MyEvent就是继承自Event的类型。
        let targetMouseEvent: MyEvent = {
            mess: 'ok',
            ...e
        }  
    }

3.2 常见的事件类型

总的事件 typescript/lib/lib.dom.d.ts


    interface GlobalEventHandlersEventMap {
        "abort": UIEvent;
        "animationcancel": AnimationEvent;
        "animationend": AnimationEvent;
        "animationiteration": AnimationEvent;
        "animationstart": AnimationEvent;
        "auxclick": MouseEvent;
        "beforeinput": InputEvent;
        "blur": FocusEvent;
        "cancel": Event;
        "canplay": Event;
        "canplaythrough": Event;
        "change": Event;
        "click": MouseEvent;
        "close": Event;
        "compositionend": CompositionEvent;
        "compositionstart": CompositionEvent;
        "compositionupdate": CompositionEvent;
        "contextmenu": MouseEvent;
        "copy": ClipboardEvent;
        "cuechange": Event;
        "cut": ClipboardEvent;
        "dblclick": MouseEvent;
        "drag": DragEvent;
        "dragend": DragEvent;
        "dragenter": DragEvent;
        "dragleave": DragEvent;
        "dragover": DragEvent;
        "dragstart": DragEvent;
        "drop": DragEvent;
        "durationchange": Event;
        "emptied": Event;
        "ended": Event;
        "error": ErrorEvent;
        "focus": FocusEvent;
        "focusin": FocusEvent;
        "focusout": FocusEvent;
        "formdata": FormDataEvent;
        "gotpointercapture": PointerEvent;
        "input": Event;
        "invalid": Event;
        "keydown": KeyboardEvent;
        "keypress": KeyboardEvent;
        "keyup": KeyboardEvent;
        "load": Event;
        "loadeddata": Event;
        "loadedmetadata": Event;
        "loadstart": Event;
        "lostpointercapture": PointerEvent;
        "mousedown": MouseEvent;
        "mouseenter": MouseEvent;
        "mouseleave": MouseEvent;
        "mousemove": MouseEvent;
        "mouseout": MouseEvent;
        "mouseover": MouseEvent;
        "mouseup": MouseEvent;
        "paste": ClipboardEvent;
        "pause": Event;
        "play": Event;
        "playing": Event;
        "pointercancel": PointerEvent;
        "pointerdown": PointerEvent;
        "pointerenter": PointerEvent;
        "pointerleave": PointerEvent;
        "pointermove": PointerEvent;
        "pointerout": PointerEvent;
        "pointerover": PointerEvent;
        "pointerup": PointerEvent;
        "progress": ProgressEvent;
        "ratechange": Event;
        "reset": Event;
        "resize": UIEvent;
        "scroll": Event;
        "scrollend": Event;
        "securitypolicyviolation": SecurityPolicyViolationEvent;
        "seeked": Event;
        "seeking": Event;
        "select": Event;
        "selectionchange": Event;
        "selectstart": Event;
        "slotchange": Event;
        "stalled": Event;
        "submit": SubmitEvent;
        "suspend": Event;
        "timeupdate": Event;
        "toggle": Event;
        "touchcancel": TouchEvent;
        "touchend": TouchEvent;
        "touchmove": TouchEvent;
        "touchstart": TouchEvent;
        "transitioncancel": TransitionEvent;
        "transitionend": TransitionEvent;
        "transitionrun": TransitionEvent;
        "transitionstart": TransitionEvent;
        "volumechange": Event;
        "waiting": Event;
        "webkitanimationend": Event;
        "webkitanimationiteration": Event;
        "webkitanimationstart": Event;
        "webkittransitionend": Event;
        "wheel": WheelEvent;
    }

简单举个例子:


    <div @click="clickEvent"></div> 
    <div @change=changeEvent"></div> 
    <div @keyboard="KeyboardEvent" >Keyboard</div>
    
     const KeyboardEvent = (e: KeyboardEvent): void => {}
     const clickEvent = (e: MouseEvent): void => {}
     const changeEvent = (e: Event): void => {}

3.3 React中事件的类型

React中,事件类型是通过合成事件(SyntheticEvent)来定义的,并统一放在React.MouseEvent和React.KeyboardEvent等React的内置类型中的。


MouseEvent<T = Element> 鼠标事件对象
TouchEvent<T = Element> 触摸事件对象
ChangeEvent<T = Element> Change 事件对象
ClipboardEvent<T = Element> 剪贴板事件对象
DragEvent<T = Element> 拖拽事件对象
WheelEvent<T = Element> 滚轮事件对象
AnimationEvent<T = Element> 动画事件对象
TransitionEvent<T = Element> 过渡事件对象

为了兼容类型,建议最好多使用框架提供的类型声明


const startDrag = (e: React.MouseEvent<HTMLDivElement, MouseEvent>): void => {}
const onDragStart = (event: React.DragEvent<HTMLDivElement>: void => {}

标签:string,dom,MouseEvent,ts,Event,HTMLElement,类型,input,event
From: https://www.cnblogs.com/xm0328/p/18144028

相关文章

  • Pyecharts制作动态GDP柱状图
    学习使用pyecharts制作动态柱状图使用csv模块进行csv数据文件处理importcsvfrompyecharts.chartsimportBar,Timelinefrompyecharts.optionsimport*frompyecharts.globalsimportThemeTypedefdealCSVFile():"""读取处理csv数据文件:retu......
  • TSINGSEE青犀算法中台消防通道堵塞/占压AI检测算法的介绍及应用
    消防通道是建筑物内用于紧急疏散的通道,其畅通无阻对于保障人员生命安全至关重要。然而,由于各种原因,消防通道经常会被杂物、车辆等堵塞,一旦发生火灾等紧急情况,后果不堪设想。为了有效解决这一问题,我们提出了一种基于人工智能技术的消防通道堵塞占用检测算法。该算法利用深度学习技......
  • Alibaba Cloud Linux 3.2104 LTS 安装php-5.6.12
    1把php安装包上传到服务器2安装php所需要的扩展yum-yinstalllibxml2libxml2-developensslopenssl-develbzip2bzip2-develcurlcurl-devellibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibmcryptlibmcrypt-develgdgd-devel3安装......
  • pandas读取sql文件出现:告警UserWarning: pandas only supports SQLAlchemy connectabl
    ​错误原因:导入sql的方式更新了解决方法:importpandasaspdfromsqlalchemyimportcreate_engineMYSQL_HOST='localhost'MYSQL_PORT='3306'MYSQL_USER='root'MYSQL_PASSWORD='123456'MYSQL_DB='cldk_data'engine=......
  • Alibaba Cloud Linux 3.2104 LTS 安装mysql5.7.39
    1上传mysql安装包到linux服务器tar-zxvfmysql-5.7.39-linux-glibc2.12-x86_64.tar.gzmvmysql-5.7.39-linux-glibc2.12-x86_64mysql5.72创建mysql用户groupaddmysqluseradd-gmysql-s/sbin/nologinmysqlchown-Rmysql:mysqlmysql5.7 ......
  • 获取AWS lightsail Windows server RDP密码
    场景创建lightsail的linuxserver时已经生成SSHkey,建立Windows的实例(Instance)时,并未提示输入管理员密码。登录时,找密码登录,提示“DecipheryourpasswordYouusedthe"keyname"keywhenyoucreatedthisinstance.Seetheinstructionstodecipherthepasswordfromthe......
  • 华为云CodeArts IDE For Python 快速使用指南
    本文分享自华为云社区《华为云CodeArtsIDEForPython快速使用指南》,作者:为云PaaS服务小智。CodeArtsIDE带有Python扩展,为Python语言提供了广泛的支持。Python扩展可以利用CodeArtsIDE的代码补全、验证、调试和单元测试等特性,与多种Python解释器协同工作,轻松切换包......
  • KG2Instructions 和 KG2Prompts 将知识图谱转换为自然语言提示
     KG2Prompts是什么?KG2Prompts是一个用于将知识图谱转换为自然语言提示的工具。它使用预训练的语言模型来生成提示,这些提示可以用于各种任务,例如文本生成、问答和摘要。KG2Prompts的工作原理如下:首先,它将知识图谱转换为一个图结构,其中节点代表实体,边代表实体之间的关系。......
  • GPT-SoVITS 部署
    GPT-SoVITShttps://github.com/RVC-Boss/GPT-SoVITShttps://www.yuque.com/baicaigongchang1145haoyuangong/ib3g1egitclonehttps://github.com/RVC-Boss/GPT-SoVITS.gitcondacreate-nGPTSoVitspython=3.9condaactivateGPTSoVitsbashinstall.shsudovimstartup.s......
  • java使用Workbook workbook = new XSSFWorkbook(inputStream);导出数据频繁GC
    由于xlsx底层使用xml存储,占用内存会比较大,官方也意识到这个问题,在3.8版本之后,提供了SXSSFWorkbook来优化写性能原来代码Workbookworkbook= newXSSFWorkbook(inputStream);优化后代码Workbookworkbook= newSXSSFWorkbook(newXSSFWorkbook(inputStream));此处有坑,请往......