2010-08-23 41 views
1

我正在使用Picasa Web Integrator(PWI)代碼,該代碼允許我使用Picasa帳戶呈現圖片庫。用戶使用表單寫入關鍵字,然後代碼創建一個div並調用PWI。如何動態刪除div並清除內存,以便使用jQuery添加具有相同ID但不同內容的新div?

它完美的工作,但我試圖添加一個「後退」按鈕,以便讓用戶選擇不刷新的關鍵字。

但代碼似乎沒有清除內存,結果與第一次相同。

下面是代碼:

//The button that the user presses when he has written the keyword. 
    $("#boton2").click(function() { 
      //Creates a new div 
      $(".shop3").append("<div></div>"); 
      $('.shop3 > div').attr('id', 'container3'); 
      //gets the keyword and creates a variable called "competidor" 
      competidor = $('#competidor_txt').val(); 
      //calls for the PWI code... 
      $("#container3").pwi({ 
        username: 'davidagnino', 
        mode: 'keyword', 
        ... 
        //This part works perfectly. 

//The "back" button that should delete the div called #container3 
    $('#back3').click(function() { 
      event.preventDefault(); 
      competidor=null; 
      delete competidor; 
      $("#container3").empty();  //should make my div empty 
      $("#container3").remove(); //deletes the div... 
    }); 
+0

你可以發佈一個鏈接到你的網頁(代碼)? – 2010-08-23 17:55:07

回答

1

我覺得這裏最好的辦法是改變div的ID上的蒼蠅,從而將其設置爲一個全新的DIV各一次。

我會設置一個計數器變量(理想情況下靜態的,而是全球性,如果這是在你的頭上):

var divCounter=0; 
$("#container"+divCounter).pwi({/*...*/}); 

當它的時間摧毀,增量divCounter併產生一個全新的股利。應該完成工作!

顯然,在所有的事件處理你提到它一般:

$("#container"+divCounter); 

快速注意,刪除一些完全從DOM中刪除,從而排空在相同的操作。這也是很好的jQuery實踐鏈接你的功能在一起,像這樣:

$("#container3").append(/*whatever*/).attr(/*whatever*/);