[Javascript] DOM alias $, $$

const $ = () => document.querySelector.call(this, arguments);
const $$ = () => document.querySelectorAll.call(this, arguments);
HTMLElement.prototype.on = (a, b, c) => this.addEventListener(a, b, c);
HTMLElement.prototype.off = (a, b) => this.removeEventListener(a, b);
HTMLElement.prototype.$ = (s) => this.querySelector(s);
HTMLElement.prototype.$ = (s) => this.querySelectorAll(s);

 

Previously:

const navbar = document.querySelector("nav")

Now:

$("nav")

 

posted @ 2023-07-18 20:41  Zhentiw  阅读(19)  评论(0编辑  收藏  举报