2016-08-14 39 views
-1
var currentYear;    
while (i < months.length) { 
       annualMonths.push("<h4>" + months[i] + "</h4><span id='monthDates'><ul class='datum monthselection'>" + daysInMonth(i, currentYear) + "</ul></span><br>"); 
       //$(".monthName").replaceWith("<h4>" + months[i] + "</h4><span id='monthDates'><ul class='datum monthselection'>" + daysInMonth(i, currentYear) + "</ul></span><br>"); 
       i++; 
      } 
      $("#calendar .currentYear .monthName").html(annualMonths); 
      i =1; 

此功能显示一年中所有月份的所有编号天数。如何替换元素的数组,而不是在jQuery中的函数调用他们加入他们?

问题是,我希望每次调用该函数来替换现有数据,而不是在DOM末尾加入连接。

这怎么可能?

回答

0

可以使用empty()方法等清除设定从DOM匹配元素的所有子节点:

function displayCalendar(currentYear) { 
    var i = 1; 
    var currentYear; 

    // Clear the array before processing again 
    annualMonths = []; 

    while (i < months.length) { 
    annualMonths.push("<h4>" + months[i] + "</h4><span id='monthDates'><ul class='datum monthselection'>" + daysInMonth(i, currentYear) + "</ul></span><br>"); 
    i++; 
    } 

    $("#calendar .currentYear .monthName").empty().html(annualMonths); 
    i = 1; 
} 
+0

尝试,但遗憾的是不工作! – Naser

+0

@Naser更新代码。尝试一次。 –

+1

它工作!非常感谢! – Naser

相关问题