2012-02-06 30 views
0

我想刪除一個對象並將其存儲(以防用戶想要稍後檢索它)。我試圖存儲在變量中的對象像它下面的線說:試圖刪除和存儲與對象detach()

How to I undo .detach()?

detach()不會從DOM刪除元素或儲存。我也沒有收到任何錯誤消息。下面是我使用的分離元素代碼:

function MMtoggle(IDnum) { 
     var rowID = "row" + IDnum; 
     var jRow = '#' + rowID; 
     thisMMbtn = $(jRow).find(".addMMbtn"); 
     var light = false; 
     var that = this; 
     if (light == false) { 
      thisMMbtn.bind("click", 
       function() { 
        var thisRow = $(this).closest(".txtContentRow"); 
        var thisTxt = thisRow.find(".txtContent"); 
        var cellStr = '<div class = "mmCell prep"></div>'; 
        $(cellStr).appendTo(thisTxt); 
        $(this).unbind("click"); 
        light = true; 
       } 
      ); 
     } 
     else { 
      thisMMbtn.bind("click", 
       function() { 
        var thisRow = $(this).closest(".txtContentRow"); 
        thisMM = thisRow.find(".mmCell"); 
        SC[rowID].rcbin = thisMM.detach(); //here is where I detach the div and store it in an object 
        $(this).unbind("click"); 
        light = false; 
       } 
      ); 
     } 
    } 

    MMtoggle(g.num); 

問題的小提琴是在這裏:http://jsfiddle.net/pScJc/

(即分離是在右邊的「+」按鈕,該按鈕它應該添加一個div,然後在再次點擊時將其分開)

回答

0

查看您的代碼我不認爲您需要detach來實現您的目標。

請嘗試此代碼。

thisMMbtn.bind("click", 
     function() { 
      var thisRow = $(this).closest(".txtContentRow"); 
      var thisTxt = thisRow.find(".txtContent"); 
      var $mmCell = thisTxt.find('.mmCell'); 
      if($mmCell.length == 0){ 
       $mmCell = $('<div class = "mmCell prep"></div>') 
          .appendTo(thisTxt).hide(); 
      } 
      $mmCell.toggle(); 
      //$(this).unbind("click"); 
     } 
    ); 

Demo

+0

@ ShankarSangoli - 有趣的...但只是出於好奇,我有沒有使用'分離()'不正確? – dopatraman 2012-02-06 21:30:47

+0

你的'detach'代碼沒有被執行,因爲你正在解綁''click'處理程序。 – ShankarSangoli 2012-02-06 21:44:08