노드를 클릭할때마다 클래스를 토글시키는법
웹/javascript2017. 12. 11. 20:17
div.onclick = function(){
if ( this.getAttribute("class").indexOf("image-selected") == -1){
this.setAttribute("class","image image-selected")
}else{
this.removeAttribute("class","image image-selected")
}
// 위의 if문은 아래 .toggle() 로 줄일 수 있다
this.classList.toggle("image-selected")
}
'웹 > javascript' 카테고리의 다른 글
모솔 몇일차인지 구하기.... (0) | 2018.01.03 |
---|---|
띠 구하기 (0) | 2018.01.03 |
2017.11.29 (0) | 2017.11.29 |