본문 바로가기

bind click event loop index

web/javascript by 낼스 2019. 7. 12.
Click Me!
Click Me!
Click Me!
Click Me!
Click Me!
Click Me!

var g = document.getElementById('my_div');
for (var i = 0, len = g.children.length; i < len; i++)
{

(function(index){
    g.children[i].onclick = function(){
          alert(index)  ;
    }    
})(i);

}

 

https://jsfiddle.net/softm/en067grx/

'web > javascript' 카테고리의 다른 글

Javascript arrow function expression  (0) 2019.07.12
Replace Tag(Script & div & B tag to strong)  (0) 2019.07.12
Javascript Async, Await 심화  (0) 2019.07.12
Javascript Async return Promise For await  (0) 2019.07.12
ECMA6  (0) 2019.01.30

댓글