日期:2014-05-16  浏览次数:20413 次

jquery 1.7 实现时间显示兼容IE6
我想实现如图的功能:
 2012年 7月 20日 第29周
js部分代码如下所示:
JScript code

$(document).ready(function() {
    var myDate = new Date();
    var yearNow = myDate.getFullYear();
    var monthNow = myDate.getMonth() + 1;
    var dayNow = myDate.getDate();
    var weekIndex = GetWeekIndex(myDate);        
    document.getElementById("year").innerHTML = yearNow;
    document.getElementById("month").innerHTML = monthNow;
    document.getElementById("day").innerHTML = dayNow;
    document.getElementById("weekIndex").innerHTML = weekIndex;    
});


jsp代码部分如下所示:
[code=HTML]
  <span style="color: red;" id="year"></span>年
  <span style="color: red;" id="month"></span>月
  <span style="color: red;"id="day"></span>日&nbsp;&nbsp;&nbsp;&nbsp;
  第<span style="color: red;" id="weekIndex"></span>周
[code]
怎样写jquery方法来实现时间显示兼容IE6呢,在线等高手指点...

------解决方案--------------------
IE6,IE7,IE8,火狐14.0都通过:
JScript code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery.js"></script>
</head>

<body>
<script type="text/javascript">
$(function() {
    var myDate = new Date();
    var yearNow = myDate.getFullYear();
    var monthNow = myDate.getMonth() + 1;
    var dayNow = myDate.getDate();
    var weekIndex = GetWeekIndex(myDate);
    $('#year').html(yearNow);
    $('#month').html(monthNow);
    $('#day').html(dayNow);
    $('#weekIndex').html(weekIndex);
});
function GetFirstWeekBegDay(year) {
    var tempdate = new Date(year, 0, 1);
    var temp = tempdate.getDay();
    temp = temp == 0 ? 7 : temp;
    tempdate = tempdate.setDate(tempdate.getDate() + (8 - temp));
    return new Date(tempdate);
}
function GetWeekIndex(dateobj) {
    var firstDay = GetFirstWeekBegDay(dateobj.getFullYear());
    if (dateobj < firstDay) {
        firstDay = GetFirstWeekBegDay(dateobj.getFullYear() - 1);
    }
    d = Math.floor((dateobj.valueOf() - firstDay.valueOf()) / 86400000);
    return Math.floor(d / 7) + 1;
}
</script> 
<span style="color: red;" id="year"></span>年 <span style="color: red;" id="month"></span>月 <span style="color: red;"id="day"></span>日&nbsp;&nbsp;&nbsp;&nbsp;
  第<span style="color: red;" id="weekIndex"></span>周
</body>
</html>