2013-10-26 5 views
1

동적으로 업데이트 된 요소에 수신기를 연결하면 동적으로 div 요소가 jQuery on 메서드를 사용하여 첨부되었지만 작동하지 않습니다?JQuery를 사용하여 동적으로 추가 된 요소에 이벤트 Listner를 추가 하시겠습니까?

HTML

<input class="123" type="button" value="button" /> 
<input class="123" type="button" value="button" /> 
<div id="footer"></div> 

jQuery를

$(document).ready(function() { 
    $('.onlineUsers').on('click', function() { 
     $('#footer').append("<div class=\"chatboxes\"> <div class=\"close\"><a href=\"#\">x</a></div> </div>"); 
    }); 

    $('a').on('click', function() { 
     alert("hello"); 
    }); 
}); 

답변

2

사용 .on()

요소는 Event Delegation을 사용할 필요가 .so를 동적으로 직접 이벤트를 바인딩 할 수 없습니다 추가됨에 따라 .

$('#footer').on('click','a',function() { 
    alert("hello"); 
}); 

구문

$(elements).on(events, selector, data, handler); 
+1

감사 Tushar 작동 ... –

+0

user2569221이 :) 도와 드리겠습니다 오신 것을 환영합니다 @ –

관련 문제