LoginSignup
2
0

More than 5 years have passed since last update.

Element.prototype jQueryLike

Last updated at Posted at 2018-04-05

usage

E();//boot one;
let log=function(ev){console.log(ev)};
fn.ce('label')
 .once('click mouseover',log)
 //.on('click mouseover',log)
 .ef('orange',log)
 .a2(document.body)
 .attr('title','xyz')
 .textContent='zzzzz'
;
.orange{
 color:orange;
 animation:x ease-in 10s; 
}
@keyframes x{
 0%{background-color:red}
 100%{background-color:orange}
}

core

;(function(root){
 /*v0.01 animation ok. but transiton the issue.*/
 let fn=root.fn||{}
 ,a='oTransitionEnd mozTransitionEnd webkitTransitionEnd transitionend'
 ,b='animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd'
 ,f=function(a,b){return function(ev){this.classList.remove(a);if(b)b(ev)}}
 ,f1=function(str){return str.split(' ').filter(d=>(d.trim().length!=0))}
 ;
 fn.r=(d=>d.parentNode.removeChild(d))
 fn.q=(s,doc=document)=>{return doc.querySelector(s)};
 fn.qa=(s,doc=document)=>{return [].slice.call(doc.querySelectorAll(s))}
 fn.ce=(d=>document.createElement(d))
 ;
 function entry(){
  root.fn=fn;
  var e=Element.prototype;
  e.on=function(a,b,c){ f1(a).map(d=>this.addEventListener(d,b,c));return this}
  e.off=function(a,b,c){ f1(a).map(d=>this.removeEventListener(d,b,c));return this}
  e.emit=function(a){ f1(a).map(d=>this.dispatchEvent(d));return this}
  e.once=function(a,b){this.on(a,b,{once:true});return this}
  e.one=e.once;
  e.a2 =function(p){p.appendChild(this);return this} //appendTo
  e.p2 =function(p){p.insertBefore(e.el,p.firstChild); return this} //prependTo
  e.as2 =function(p){p.parentNode.insertBefore(this,p.nextSibling);return this} //append siblingsTo
  e.ps2 =function(p){p.parentNode.insertBefore(this,p);return this} //prepend siblingsTo
  e.r=function(){return fn.r(this)}
  e.q=function(s){return fn.q(s,this)}
  e.qa=function(s){return fn.qa(s,this)}
  e.ce=function(s){return fn.ce(s,this)}
  //
  e.ac=function(a){f1(a).map(d=>this.classList.add(d));return this} 
  e.rc=function(a){f1(a).map(d=>this.classList.remove(d));return this}  
  e.attr=function(a,b){ if(b){ this.setAttribute(a,b);return this}else{return this.getAttribute(a)} }
  e.rattr=function(a){ this.removeAttribute(a);return this}
  /*v0.01 animation only*/
  e.ef=function(cls,caller){this.classList.add(cls);this.once(a+' '+b,f(cls,caller));return this}  
 }
 ;
 root.E=entry;
})(this);

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0