1

有人可以向我解释为什么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 事件,该事件设置为countDownClock1 分钟后显示

<body onload = "setInterval('countDownClock()', 60000)">
4

1 回答 1

2

啊哈,编辑使一切变得不同。

一个页面只能有一个load事件处理程序,因此如果您使用该属性添加一个处理程序,window.onload然后使用该属性添加另一个处理程序,则body onload只有其中一个可以工作。

将两个操作放在同一个处理程序中:

window.onload = function(){
  countDownClock();
  window.setInterval(countDownClock, 60000);
};
于 2012-09-03T23:42:27.747 回答