2013-03-11 92 views
0

我將一個xml文件保存到一個json對象中,然後循環顯示它在一個容器中,然後嘗試點擊標題但沒有任何反應。我的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'。 – 2013-03-11 11:07:59

回答

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 2013-03-11 11:22:38

+0

weclome ....很高興它幫助..快樂編碼... – bipen 2013-03-11 11:24:49