js Chain
// var e = $(".class div").elm; // -> Element // -> <div></div> // $(".class div").atr("class", "x").atr("background-color: red;").in("<p>text</p>"); // -> <div class="x" style="background-color: red;"><p>text</p></div> // $(".class div").atr("style"); // -> <div class="x"><p>text</p></div> // $(".class div").add("new"); // -> <div class="x"><p>text</p>new</div> var $ = function(v, n = false) { if (!(this instanceof $)) { return new $(v, n); }else { if(typeof v == "object") { this.elm = v; } else if(typeof v == "string") { var qsa = (v) => { var e = document.querySelectorAll(v); var el = e.length; if(el) { if(el == 1) { return e[0]; }else { return e; } }else { return undefined; } } if(n == true) { this.elm = document.createElement(v); }else { this.elm = qsa(v); } } } }; $.prototype = { e: function(v) { if(typeof v == "number") { this.elm = this.elm[v]; } return this; }, atr: function(v1, v2) { if(this.elm.length) { for(var i = 0; i < this.elm.length; i++) { if(v1 !== undefined && v2 == undefined) { if(v1.match(/[^\:\;]*?\:[^\:\;]*?\;/g)) { var a = v1.split(";").filter(v => (v !== ";" && v)), b; for(var j = 0; j < a.length; j++) { b = a[j].replace(/^ |[ ]{2}|\n/g, "").split(/\: ?/); this.elm[i].style[b[0].replace(/\-./g,function(s) { return s.charAt(1).toUpperCase(); })] = b[1]; } }else { this.elm[i].removeAttribute(v1); } } else if(v2 !== undefined) { this.elm[i].setAttribute(v1, v2); } } }else { if(v1 !== undefined && v2 == undefined) { if(v1.match(/[^\:\;]*?\:[^\:\;]*?\;/g)) { var a = v1.split(";").filter(v => (v !== ";" && v)), b; for(var j = 0; j < a.length; j++) { b = a[j].replace(/^ |[ ]{2}|\n/g, "").split(/\: ?/); this.elm.style[b[0].replace(/\-./g,function(s) { return s.charAt(1).toUpperCase(); })] = b[1]; } }else { this.elm.removeAttribute(v1); } } else if(v2 !== undefined) { this.elm.setAttribute(v1, v2); } } return this; }, app: function(v) { if(this.elm.length) { for(var i = 0; i < this.elm.length; i++) { var x = v.cloneNode(true); this.elm[i].appendChild(x); } }else { var x = v.cloneNode(true); this.elm.appendChild(x); } return this; }, in: function(v) { if(this.elm.length) { for(var i = 0; i < this.elm.length; i++) { this.elm[i].innerHTML = v; } }else { this.elm.innerHTML = v; } return this; }, add: function(v) { if(this.elm.length) { for(var i = 0; i < this.elm.length; i++) { this.elm[i].innerHTML += v; } }else { this.elm.innerHTML += v; } return this; }, prev: function() { this.elm = this.elm.previousElementSibling; return this; }, next: function() { this.elm = this.elm.nextElementSibling; return this; }, parent: function() { this.elm = this.elm.parentNode; return this; }, class: function(v) { var x = v.split(/\,| |\n/).filter(v => v); if(this.elm.length) { for(var i = 0; i < this.elm.length; i++) { for(var j = 0; j < x.length; j++) { this.elm[i].classList.toggle(x[j]); } } }else { for(var i = 0; i < x.length; i++) { this.elm.classList.toggle(x[i]); } } return this; }, // non-chain has: function (v){ var x = v.split(/, ?/).filter(v => v), y, z; for(var i = 0; i < x.length; i++){ if(x[i].match(/^\-/g)) { y = x[i].replace(/^-/g, ""); z = !this.elm.hasAttribute(y); } else if(x[i].match(/^\+/g)) { y = x[i].replace(/^+/g, ""); z = this.elm.hasAttribute(y); }else { z = this.elm.hasAttribute(x[i]); } if(!z) return false; } return true; }, get: function (v){ if(this.elm.length) { var a = []; for(var i = 0; i < this.elm.length; i++){ a.push(this.elm[i].getAttribute(v)); } return a; }else { return this.elm.getAttribute(v); } }, del: function (v){ if(this.elm.length) { for(var i = 0; i < this.elm.length; i++){ this.elm[i].parentNode.removeChild(this.elm[i]); } }else { this.elm.parentNode.removeChild(this.elm); } } };
page revision: 41, last edited: 25 Sep 2019 06:28