본문 바로가기
Front-End/jQuery

[jQuery] on('click') & click Event

by 김뚱 2019. 5. 28.

on('click')은 element에 동적으로 이벤트를 바인딩이 가능하지만 click은 처음에 선언된 element에만 동작한다.

 

여기서 처음에 선언된 element는 맨 처음 페이지를 로딩하였을 때 선언되어 있는 element를 의미한다.

 

즉 on('click')은 페이지에서 동적으로 새로운 태그들이 추가되어도 이벤트를 줄 수 있다.

 

 

Test code

$('#test').on('click', function(){
	alert('test success');
});

 

$(document).on('click', id, function) 와 같은 형태로도 사용할 수 있다.

$(document).on('click', '#test', function(e) {
	var tar = e.currentTarget;
    var row = tar.closest('tr');
    var num = row.getElementsByClassName('td-number')[0].textContent;    
});

 

 

728x90
반응형

'Front-End > jQuery' 카테고리의 다른 글

[jQuery] 장바구니 & checkbox  (0) 2020.04.04
[jQuery] data()  (0) 2019.05.28
[jQuery] select option 설정  (0) 2019.05.27
[jQuery] jQuery Plugins 참고 사이트  (0) 2019.05.23
[jQuery] checkbox relation 만들기 (dataset object concept)  (0) 2018.09.09

댓글