[英]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.