2012-12-13 124 views
0

我正在使用jQuery UI Datepicker。如何在click事件中將元素添加到元素上。jQuery UI Datepicker。

以下是我的代碼。我究竟做錯了什麼?

$(function(){ 

    $('#calendar').datepicker(); 
    $('#calendar').children('td').click(function(){ 
     $(this).addClass('selected'); 
    }); 

}); 
+0

你真的需要設置類*選擇*?相反,您可以在類* ui-datepicker-current-day *上繼承並設置您的樣式。 –

+0

試試這個$('#calendar')。children('td')。addClass('selected'); –

+0

@anujarora這會將該類添加到*所有*'​​'元素(如果它工作的),我猜測OP只希望它在選定的日期。 –

回答

0

這裏有一個快速的解決方案,應該讓你開始,反正:

var selectedDay = new Date().getDate(); 

$(function() { 
    $("#calendar").datepicker({ 
     onSelect: function(dateText, inst) { 
      // Save the selected day on select (if you show other months dates, you should save the selected month also.) 
      selectedDay = inst.selectedDay; 
     }, 
     // This is run for every day visible in the datepicker. 
     beforeShowDay: function (date) { 
      if (date.getDate() == selectedDay) { 
       return [true, "selected", ""]; // Here you set your css class 
      } else { 
       return [true, ""] 
      } 
     } 
    }); 
});​ 

beforeShowDay功能將採取設置的CSS類的保健和其他天將其刪除。

當然,您可以擴展此選項以使用多個選定的日期,這只是爲了顯示您可以開始的位置。

+0

謝謝馬里奧的幫助!試圖自己解決。 – Giffo