我试图通过单击下一个和上一个链接在 div 中动态打印下个月和上个月。尽管该值已正确生成,但该值并未打印在<div id="month"></div>
. 我要去哪里错了。我在用着$("#month").html(curmonthYear);
<html>
<head>
<script type="text/javascript">
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var cur_dat = new Date();
var curYear = cur_dat.getFullYear();
var curMonth = cur_dat.getMonth();
var curMonthYear = month[cur_dat.getMonth()] + ", " + cur_dat.getFullYear();
function monthNav(nav) {
if(nav == 'prev') {
var newMonth = cur_dat.getMonth() - 1;
var newYear = cur_dat.getFullYear();
if(newMonth < 0) {
newMonth = 11;
newYear -= 1;
}
cur_dat.setMonth(newMonth);
cur_dat.setFullYear(newYear);
curMonthYear = month[newMonth] + ", " + newYear;
alert(curMonthYear); // gives correct date
$("#month").html(curMonthYear); // but doesn't prints here.
//alert("Prev");
} else {
var newMonth = cur_dat.getMonth() + 1;
var newYear = cur_dat.getFullYear();
if(newMonth > 11) {
newMonth = 0;
newYear += 1;
}
if(newYear == curYear && newMonth > curMonth) {
return false;
}
cur_dat.setMonth(newMonth);
cur_dat.setFullYear(newYear);
curMonthYear = month[newMonth] + ", " + newYear;
alert(curMonthYear); // gives correct date
$("#month").html(curMonthYear); // but doesn't prints.
}
}
</script>
</head>
<body>
<a href="#" onclick="monthNav('prev')">Prev</a>
<div id="month">abc</div>
<a href="#" onclick="monthNav('next')">Next</a>
</body>
</html>