chore: Update __Query class to return null when no element is found

This commit is contained in:
Max 2024-08-06 16:03:00 +08:00
parent ad7954dbb2
commit fba0a865dc
2 changed files with 72 additions and 64 deletions

File diff suppressed because one or more lines are too long

View file

@ -43,12 +43,20 @@ class __Query {
return this.elements; return this.elements;
} }
find(selector: string): __Query { find(selector: string): __Query | null {
return new __Query(this.element?.querySelector(selector) || ""); const elm = this.element?.querySelector(selector);
if (elm) {
return new __Query(elm);
}
return null;
} }
closest(selector: string): __Query { closest(selector: string): __Query | null {
return new __Query(this.element?.closest(selector) || ""); const elm = this.element?.closest(selector);
if (elm) {
return new __Query(elm);
}
return null;
} }
on(event: string, callback: (event: Event) => void): __Query { on(event: string, callback: (event: Event) => void): __Query {