@truenewx/tnxcore
Version:
互联网技术解决方案:JavaScript核心扩展支持
32 lines (31 loc) • 1.16 kB
text/typescript
Object.assign(Element.prototype, {
getFirstChildWithoutTagName: function (this: Element, tagName: string): Element | undefined {
const children = this.childNodes as any;
for (let i = 0; i < children.length; i++) {
if (children[i].tagName && children[i].tagName !== (tagName as any).toUpperCase()) {
return children[i];
}
}
return undefined;
},
toggleClass: function (this: Element, className: string): void {
if (this.classList.contains(className)) {
this.classList.remove(className);
} else {
this.classList.add(className);
}
},
getParentIf: function (this: Element, predicate?: (el: Element) => boolean): Element | null {
let parent = this.parentElement;
if (typeof predicate === 'function') {
while (parent) {
if ((predicate as any)(parent) !== false) {
return parent;
}
parent = parent.parentElement;
}
return null;
}
return parent;
},
});