2011-06-10 102 views
1

我遇到了jQuery和.clone(true, true)的問題。看看on this jsFiddle用jQuery克隆HTMLElements奇怪地失敗

問題是:當我克隆一個對象(使用.clone(true, true) - deep:數據和事件)時,這些事件可以工作,但是在原始對象(模型對象)上應用所有函數。

在閱讀代碼時都會清楚。

再見和感謝所有幫助:)

+0

什麼是你想實現什麼呢? – kroehre 2011-06-10 18:30:26

回答

2

我相信這個問題是你的大量使用exampleVariable = $(this)

當您使用變量而不是明確使用$(this)時,如果沒有任何意義,則不使用當前的$(this)

我做了一些改變:(小提琴這裏:http://jsfiddle.net/PGM6W/

 // On click on more, append a new model 
     // Will update table buttons too 
     // THIS WORKS FINE, except if I click on remove and click on this two times (try it) 
     selfRow.find('a.more').click(function(){ 
      $(this).parents("table").append(model.clone(true, true)); 
      updateModel(selfTable); 
     }); 

     // On click on remove, will remove current row 
     // Will update table buttons too 
     // THIS NOT WORKS FINE, and broke the a.more event! 
     selfRow.find('a.remove').click(function(){ 
      $(this).remove(); 
      updateModel(selfTable); 
     }); 
+0

太棒了!我做了一些修改,使其工作得更好,但你完全正確。感謝幫助。 [更新的代碼](http://jsfiddle.net/PGM6W/2/)。 – 2011-06-10 19:02:13