LoginSignup
0
0

More than 5 years have passed since last update.

class の変更検知 MOで

Last updated at Posted at 2019-04-20

-demo https://codepen.io/gnjo/pen/oOdOxp
-attributeだけ検知したい場合は多々ある。

let fn={}
fn.changeAttr=function(el,caller){
 let target=el
 ,ob=new MutationObserver(mu=>{ mu.map(d=>caller(d,target) )})
 ob.observe(target,{attributes: true})
 ;
 return ob;
 /*usage
let el=document.querySelector('#frame')
fn.changeAttr(el,(ev)=>{
 if(ev.attributeName==='class') //
   console.log('class change',ev.target.classList)
}) 
 */
}
;

-追記 inputのvalueは改めてsetAttributeする。 この場合valueは数字に見えても文字列。

//pug
input.x(onkeyup="this.setAttribute('value',this.value)")
input.y(type="range",onchange="this.setAttribute('value',this.value)")
0
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
0
0