2015-08-17 36 views
0

我有一個錨點標記,當被點擊時顯示更多的項目,顯示那些項目後,另一個錨點出現並再次摺疊這些項目。我有這個腳本在錨標記中的名爲evitd的屬性內搜索字符串,當它發現值觸發警報時,字符串值爲('expand_collapse'),然後顯示更多項並顯示摺疊錨,但腳本不是即使錨點的屬性evtid的值爲'expand_collapse',也可以進行第二次工作。任何建議將讚賞親切jquery停止工作的動態創建元素

$(document).ready(function(){ 
 
     $('.ms-cal-nav').each(function() { 
 
     var $this = $(this); 
 
     $this.on("click", function() { 
 
      if ($(this).attr('evtid').indexOf('expand_collapse') > -1) { 
 
      alert("hello"); 
 
     
 
      } 
 
     }); 
 
    }); 
 
});
<div class="ms-acal-ctrlitem" _expand="expand"><a href="javascript:void(0);" class="ms-cal-nav" evtid="expand_collapse"><img border="0" width="9" height="7">7 more items</a></div>

+0

看看事件代表團。 – crush

+3

[動態創建的元素上的事件綁定?]的可能重複?(http://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements) – crush

回答

0

jQuery documentation對如何做一個很好的解釋 - 他們稱之爲「委託事件」。你在一些靜態祖先元素上註冊你的監聽器(如body,但最好是更接近你想要監視的元素),然後監聽後代元素上的事件。這使您能夠處理動態添加的元素上的事件。

$(document).ready(function(){ 
    $('body').on('click','.ms-cal-nav', function() { 
    if ($(this).attr('evtid').indexOf('expand_collapse') > -1) { 
     alert("hello"); 
    } 
    }); 
}); 

注:我也刪除您的通話.each(),因爲它是不必要的。

+0

非常感謝你!!!!!!! ! – kazzius