1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| Object.assign(HTMLElement.prototype, { createChild: function(tag, obj, positon) { const child = document.createElement(tag); Object.assign(child, obj); switch (positon) { case "after": this.insertAfter(child); break; case "replace": this.innerHTML = ""; default: this.appendChild(child); } return child; }, wrap: function(obj) { const box = document.createElement("div"); Object.assign(box, obj); this.parentNode.insertBefore(box, this); this.parentNode.removeChild(this); box.appendChild(this); }, height: function(h) { if (h) { this.style.height = typeof h === "number" ? h + "rem" : h; } return this.getBoundingClientRect().height; }, width: function(w) { if (w) { this.style.width = typeof w === "number" ? w + "rem" : w; } return this.getBoundingClientRect().width; }, top: function() { return this.getBoundingClientRect().top; }, left: function() { return this.getBoundingClientRect().left; }, attr: function(type, value) { if (value === null) { return this.removeAttribute(type); }
if (value) { this.setAttribute(type, value); return this; } else { return this.getAttribute(type); } }, insertAfter: function(element) { const parent = this.parentNode; if (parent.lastChild === this) { parent.appendChild(element); } else { parent.insertBefore(element, this.nextSibling); } }, display: function(d) { if (d == null) { return this.style.display; } else { this.style.display = d; return this; } }, child: function(selector) { return $(selector, this); }, find: function(selector) { return $.all(selector, this); }, _class: function(type, className, display) { const classNames = className.indexOf(" ") ? className.split(" ") : [className]; const that = this; classNames.forEach(function(name) { if (type === "toggle") { that.classList.toggle(name, display); } else { that.classList[type](name); } }); }, addClass: function(className) { this._class("add", className); return this; }, removeClass: function(className) { this._class("remove", className); return this; }, toggleClass: function(className, display) { this._class("toggle", className, display); return this; }, hasClass: function(className) { return this.classList.contains(className); } });
|