2012-12-26 146 views
0

因此,我有一些div類與「nieuwitem」,並根據他們在工作流程上的位置(左,中,右),他們分別有一個add1類bar1,bar2,bar3。第二次點擊不工作jquery

當我點擊.nieuwitem時,它會將點擊的div更改爲絕對div並增加寬度以覆蓋其他項目,並添加類「activenews」。它還在點擊的div的位置上添加了一個無形的div,以保持剩餘邊距(所以其他項目不會混雜在一起)。

當我點擊另一個div或絕對div時,絕對div(class =「activenews」)通過刪除絕對div並使隱形div(invisitem)回到可見狀態變回正常。

但是,當我嘗試點擊第三次設置回正常的div時,因此它再次展開,它不起作用。我的意思是,沒有什麼是觸發的。甚至沒有「警報('嗨');」。

在此先感謝

<script> 
     $('.nieuwitem').click(function(){ 
      alert('hi'); 
     var dis = $(this).clone(); 


     $('.activenews').remove(); 
     $('.invisitem').animate({opacity:1}).removeClass('invisitem').removeAttr('style'); 


     if($(this).attr('class') == 'nieuwitem bar1' || $(this).attr('class') == 'nieuwitem bar2'){ 
      var offset = $(this).offset(); 

      $(this).hide(); 
      dis.insertAfter($(this)).animate({opacity:0},500).addClass('invisitem'); 

      $(this).css({ height:'320', position:'absolute', 'top' : offset.top, 'left':offset.left, 'background-image':'none', 'background-color':'rgba(255,255,255,0.1)'}).addClass('activenews').hide().animate({width:'42%', 'backgroundColor':'rgba(255,255,255,0.9)'},10).fadeIn(800).addClass('activenews'); 

     } 
     else{ 
      var offset = $(this).prev().offset(); 

      $(this).hide(); 
      dis.insertAfter($(this)).animate({opacity:0},500).addClass('invisitem'); 

      $(this).css({ height:$(this).height(), position:'absolute', 'top' : offset.top, 'left':offset.left, 'background-image':'none', 'background-color':'rgba(255,255,255,0.1)'}).addClass('activenews').hide().animate({width:'42%', 'backgroundColor':'rgba(255,255,255,0.9)'},10).fadeIn(800).addClass('activenews'); 

     } 

     } 


    ); 

</script> 

回答

3

這是因爲你被克隆的元素,但不是元素的數據和事件(即click事件

如果更改:

var dis = $(this).clone(); 

var dis = $(this).clone(true); 

它應該工作; check the docs。你的代碼中還有其他一些奇怪的東西(比如多次添加同一個類),但是你問上述問題的答案是:

p.s.歡迎來到SO:D

+1

檢查多個類的文字'class'屬性也是不安全的。不能保證它包含那些精確順序的類。最好使用['.hasClass()'](http://api.jquery.com/hasClass/)。 – JJJ

+0

@Juhana,這是一個很好的觀點。更好地評論這個問題,而不是這個未被接受的答案,雖然 – lnrbob

+0

@Stvdd不擔心這兩個問題:) – lnrbob