2013-03-11 3 views
0

json 객체에 XML 파일을 저장 한 다음 제목을 반복하여 컨테이너에 표시 한 다음 제목을 클릭하려고했지만 아무 것도 발생하지 않습니다.jquery 내 클릭 이벤트가 발생하지 않음

$(document).ready(function() { 
$.get("Titles.xml", function(data, status) { 
    var jsonObj = $.xml2json(data); 
    $("#videoListTmpl").tmpl(jsonObj).appendTo("#titlesContainer"); 
}); 

$(".videoItem").on('click', function() { 
    console.log("this is the click"); 
}); 

HTML 파일.

<body> 
<div id="container" style="margin-right: auto;margin-left: auto;width:960px;clear:both"> 
<div style="margin:0 auto;width:260px;float:left;height:400px;overflow:scroll" id="titlesContainer"></div> 
<div style="margin:0 auto;width:670px;float:right;" id="titleContainer"></div> 
</div> 
<script id="videoListTmpl" type="text/x-jquery-tmpl"> 
    {{each Titles}} 
     <div class="videoItem" style="cursor:pointer"> 
      ${titles} 
     </div> 
    {{/each}} 
</script> 

이벤트가 실행되지 않는 이유에 대한 조언이 있으십니까? 위임에

+0

코드가 정상적으로 보입니다. 'on '함수를'document.ready'에 추가하십시오. –

답변

2

시도 ..

$(document).on('click',".videoItem", function() { 
    console.log("this is the click"); 
}); 

또는 문서에 존재하는 가장 가까운 상위 요소에 위임 ... 당신이에 대한 자세한 내용을 읽으려면

$("#titlesContainer").on('click',".videoItem", function() { 
    console.log("this is the click"); 
}); 

link 통과 이벤트시

+0

고마워, 많은 감사를 – Arianule

+0

weclome .... 다행히 도움이 .. 행복한 코딩 ... – bipen

관련 문제