function $(...args) {
return new Jq(...args);
}
class Jq {
constructor(selector, root) {
this.pervObject = root || $(document, {})
if (typeof selector === 'string') {
let dom = document.querySelectorAll(selector);
this.getDom(dom);
} else if (typeof selector === 'object') {
this.getDom(selector);
} else if (typeof selector === 'function') {
window.addEventListener("DOMContentLoaded", selector)
}
}
getDom(selector) {
if (selector.length === undefined) {
this[0] = selector;
this.length = 1;
} else {
for (let i = 0; i < selector.length; i++) {
this[i] = selector[i];
}
this.length = selector.length;
}
}
eq(index) {
return $(this[index], this);
}
end() {
return this.pervObject;
}
click(cb) {
for (let i = 0; i < this.length; i++) {
this[i].addEventListener("click", cb);
}
return this;
}
on(events, cb) {
events = events.split(" ").filter(item => item);
for (let i = 0; i < events.length; i++) {
for (let j = 0; j < this.length; j++) {
this[j].addEventListener(events[i], cb);
}
}
return this;
}
css(...args) {
if (typeof args[0] === 'string') {
if (args[1]) {
Jq.setStyle(this[0], args[0], args[1])
} else {
return Jq.getStyle(this[0], args[0])
}
} else if (typeof args[0] === 'object') {
for (const key in args[0]) {
Jq.setStyle(this[0], key, args[0][key])
}
}
return this;
}
static getStyle(el, attr) {
if ($.cssHooks && attr in $.cssHooks) {
return $.cssHooks[attr].get(el);
}
return getComputedStyle(el)[attr];
}
static setStyle(el, attr, val) {
if ($.cssHooks && attr in $.cssHooks) {
$.cssHooks[attr].set(el, val);
} else {
el.style[attr] = val;
}
}
}