[英]How to add same jquery to different div's but with different content?
[英]How to dynamically delete a div and clear the memory so I could add a new div with the same ID but different content using jQuery?
我正在使用Picasa網絡集成商(PWI)代碼,該代碼使我可以使用picasa帳戶展示圖片庫。 用戶使用表單編寫一個關鍵字,然后代碼創建一個div並調用PWI。
它運行完美,但是我試圖添加一個“后退”按鈕,以使用戶無需刷新即可選擇其他關鍵字。
但是代碼似乎並未清除內存,結果與第一次相同。
這是代碼:
//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...
});
我認為最好的方法是立即更改Div的ID,從而每次將其設置為一個新的div。
我將設置一個計數器變量(理想情況下是靜態的,但如果超出您的范圍,則為全局變量):
var divCounter=0;
$("#container"+divCounter).pwi({/*...*/});
當需要銷毀它時,增加divCounter並生成一個新的div。 應該把工作做好!
顯然,在所有事件處理程序中,通常都會引用它:
$("#container"+divCounter);
快速說明,刪除某些內容會將其完全從DOM中刪除,從而在同一操作中將其清空。 將函數鏈接在一起也是一種不錯的jQuery做法,例如:
$("#container3").append(/*whatever*/).attr(/*whatever*/);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.