简体   繁体   English

打字稿错误:类型“元素”上不存在属性“包含”。

[英]Typescript error :Property 'contains' does not exist on type 'Element'.?

Hi I am listening on a focus event on html dialog and try to check if the current focused element is part of my "dialog" class. 嗨,我在html对话框上监听焦点事件,并尝试检查当前的焦点元素是否属于我的“ dialog”类。 see the code below. 请参阅下面的代码。

$(document).ready(() => {
    document.addEventListener("focus", (event) => {
        var dialog = document.getElementsByClassName("dialog table-storage-dialog");
        var dialogOpen: boolean = true;
        var currentElement: any = event.target;
        if (dialogOpen && !dialog[0].contains(a)) {
            event.stopPropagation();
             $("#dialog table-storage-dialog").focus();
        }
    }, true);
});

but this code throws compilation error "Property 'contains' does not exist on type 'Element'". 但是此代码将引发编译错误“类型'元素'上不存在属性'包含'”。 Any idea what is wrong with this code ? 知道此代码有什么问题吗?

This code makes sure that when you keep pressing tab when the html dialog is open, focus don't go out of the dialog to background window. 此代码可确保在html对话框打开时按住Tab键时,焦点不会从对话框移到后台窗口。 it stays within the dialog 它停留在对话框中

A DOM element doesn't have a contains method, just like the error says. 就像错误提示一样,DOM元素没有contains方法。 I'm thinking you meant to use jQuery instead. 我在想您打算改用jQuery。

Change this line: 更改此行:

var dialog = document.getElementsByClassName("dialog table-storage-dialog");

to this: 对此:

var dialog = $(".dialog, .table-storage-dialog");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 Typescript 中的“元素”类型错误不存在属性? - How to fix property does not exist on type 'Element' error in Typescript? Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" 打字稿类型错误属性不存在 - Typescript type error property does not exist 触发点击 Typescript - 类型“元素”上不存在属性“点击” - Trigger click in Typescript - Property 'click' does not exist on type 'Element' 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript TypeScript构建错误:“ IStateParamsService”类型不存在属性 - TypeScript Build Error : Property does not exist on type 'IStateParamsService' "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' Typescript 错误; 'CellDetails | 类型上不存在任何属性'按钮' 内容单元更新' - Typescript error; any Property 'button' does not exist on type 'CellDetails | ContentCellUpdate'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM