有人可以向我解释为什么window.onload
在 aspx 中可以正常工作,但在 xhtml 中不行吗?
此window.onload
示例在 aspx 中运行良好:
<script type="text/javascript">
//<![CDATA[
//handles the collapse of submenu items on navigation side menu
function toggle() {
document.getElementById('node4').style.display = '';
}
window.onload = toggle;
//]]>
</script>
然而这个window.onload
例子在严格的 XHTML 1.0 中不起作用。countDownClock
(加载页面时它不会立即触发):
<script type="text/javascript">
//<![CDATA[
//handles the collapse of submenu items on navigation side menu
function countDownClock() {
today = new Date();
openingDay = new Date();
openingDay.setMonth(2, 23);
(today > openingDay) ? openingDay.setFullYear(2013) : openingDay.setFullYear();
openingDay.setHours(9, 0, 0, 0);
document.getElementById("mallclock").dayNow.value = showDate(today);
document.getElementById("mallclock").timeNow.value = showTime(today);
var daysLeft = dayDiff(today, openingDay);
var hoursLeft = hoursDiff(today, openingDay);
var minutesLeft = minutesDiff(today, openingDay);
daysLeft = ((hoursLeft - 24) >= 0) ? daysLeft + (hoursLeft / 24) : daysLeft;
hoursLeft = ((hoursLeft - 24) >= 0) ? hoursLeft - ((hoursLeft / 24) * 24) : hoursLeft;
hoursLeft = ((minutesLeft - 60) >= 0) ? hoursLeft + (minutesLeft / 60) : hoursLeft;
minutesLeft = ((minutesLeft - 60) >= 0) ? minutesLeft - ((minutesLeft / 60) * 60) : minutesLeft;
document.getElementById("mallclock").days.value = daysLeft;
document.getElementById("mallclock").hours.value = hoursLeft;
document.getElementById("mallclock").minutes.value = minutesLeft;
}
window.onload = countDownClock;
//]]>
</script>
它只触发 body 事件,该事件设置为countDownClock
1 分钟后显示
<body onload = "setInterval('countDownClock()', 60000)">