我設計了一個日曆,您可以在其中點擊日期以顯示日曆。它在除Safari之外的所有瀏覽器中都能正常工作。我搜索了幾個小時的錯誤,但我無法找到它,我在谷歌搜索說,我不得不改變日期字符串的格式,但我直接創建日期對象,而不使用日期字符串或初始化,所以那裏必須是另一種方式。你發現錯誤嗎?我會非常感謝!Javascript - 日曆在Safari上不起作用
main.html中:
<html>
<head>
<script language="javascript" type="text/javascript" src="functions.js"></script>
</head>
<body>
<script>
displayMyDate();
calendar();
</script>
</body>
</html>
functions.js:
var myDate = new Date();
function setStyle(id,style,value)
{
id.style[style] = value;
}
function opacity(el,opacity)
{
setStyle(el,"filter:","alpha(opacity="+opacity+")");
setStyle(el,"-moz-opacity",opacity/100);
setStyle(el,"-khtml-opacity",opacity/100);
setStyle(el,"opacity",opacity/100);
}
function onCalendarClick(date) {
myDate = new Date(date);
var div = document.getElementById('myDate');
if (div != null) {
div.innerHTML = 'Datum: ' + myDate.toLocaleDateString();
}
onReturnToHome();
}
function displayMyDate() {
document.write('<div id="myDate" onclick="onMyDateClick();">Datum: ' + myDate.toLocaleDateString() + '</div>');
}
function calendar(date)
{
if (date == null) var date = new Date();
else myDate =date;
var day = date.getDate();
var month = date.getMonth();
var year = date.getYear();
if(year<=200)
{
year += 1900;
}
months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December');
days_in_month = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if(year%4 == 0 && year!=1900)
{
days_in_month[1]=29;
}
total = days_in_month[month];
var date_today = day+' '+months[month]+' '+year;
beg_j = date;
beg_j.setDate(1);
if(beg_j.getDate()==2)
{
beg_j=setDate(0);
}
beg_j = beg_j.getDay();
document.write('<table class="cal_calendar" onload="opacity(document.getElementById(\'cal_body\'),20);"><tbody id="cal_body"><tr><th colspan="7">'+date_today+'</th></tr>');
document.write('<tr class="cal_d_weeks"><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr>');
week = 0;
for(i=1;i<=beg_j;i++)
{
document.write('<td class="cal_days_bef_aft">'+(days_in_month[month-1]-beg_j+i)+'</td>');
week++;
}
for(i=1;i<=total;i++)
{
if(week==0)
{
document.write('<tr>');
}
if(day==i)
{
document.write('<td class="cal_today" onClick="onCalendarClick(\''+ year + '-' + (month+1) + '-' + i + '\');">'+i+'</td>');
}
else
{
document.write('<td onClick="onCalendarClick(\''+ year + '-' + (month+1) + '-' + i + '\');">'+i+'</td>');
}
week++;
if(week==7)
{
document.write('</tr>');
week=0;
}
}
for(i=1;week!=0;i++)
{
document.write('<td class="cal_days_bef_aft">'+i+'</td>');
week++;
if(week==7)
{
document.write('</tr>');
week=0;
}
}
document.write('</tbody></table>');
opacity(document.getElementById('cal_body'),70);
return true;
}
你得到Safari瀏覽器控制檯上的錯誤? –
當你點擊另一個日期,然後他顯示無效日期 –
使用* document.write *編寫無效HTML代碼片段不是一個好主意,即使最終它是「有效的」(例如,在不同的語句中寫入表格打開標籤寫結束標記)。最好是構造整個字符串,並在完成時使用一個* document.write *。腳本元素的* language *屬性在HTML 4中被棄用,並從此被刪除。 – RobG