1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!doctype html>
<html lang="ko">
<head>
    <meta charset="utf-8">
    <title>event.data</title>
    <script src="js/jquery.js"></script>
</head>
<body>
 
    <button> 0 </button>
    <button> 1 </button>
    <button> 2 </button>
    <button> 3 </button>
    <button> 4 </button>
     
    <script>
 
    // for문의 i값 재사용을 위해
    // button에 이벤트 등록할때 event 객체에  data(key : velue)도 함께 등록
 
    for ( var i = 0; i < 5; i++ ) {
 
        $( "button" ).eq( i ).on( "click", { value: i }, function( event ) {
            console.log("button = " + $( this ).index());
            console.log("event.data.value = " + event.data.value);
            console.log("i = " + i);
        });
    }
 
    </script>
 
</body>
</html>


반응형
Posted by 힘없는염소