只需做几个月/年的切换片段。我已经完成了一半,但是如果你击中前一个箭头那一半就会出现故障。 1月之后有一个空白区域。
您可以在此处查看我的代码:
jQuery的:
var $months = [
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"];
var d = new Date();
var $m = d.getMonth() + 1;
var $y = d.getFullYear();
$('.history-months').click(function () {
var num = $('.months').attr('month-count');
var future_month = parseFloat(num) - 1;
$('.months').attr("month-count", future_month);
$('.months').text($months[future_month]);
if (num < 2) {
$('.months').attr("month-count", 13);
}
});
$('.future-months').click(function () {
var num = $('.months').attr('month-count');
var future_month = parseFloat(num) + 1;
$('.months').attr("month-count", future_month);
$('.months').text($months[future_month]);
if (num > 10) {
$('.months').attr("month-count", 0);
}
});
$('.months').text($months[$m]).attr('month-count', $m);
HTML:
<a href="#" class="history-months"><</a>
<span class="months" month-count="5">May</span>
<a href="#" class="future-months">></a>
你也可以在JSFIDDLE上查看它: http://jsfiddle.net/omerblink/4fzFD/6/
谢谢大家:)
欧麦
答案 0 :(得分:3)
您必须先检查您的条件,您的数组包含0-12
,这意味着它有13
个值,因为还有一个空值,因此您必须检查num == 1
然后你可以再次将month-count
设置为13-1 = 12
[绕过数组中的空白值]
试试这个
$('.history-months').click(function () {
var num = $('.months').attr('month-count');
var future_month = parseFloat(num) == 1 ? (13 -1) : parseFloat(num) - 1; // Condition
$('.months').attr("month-count", future_month);
$('.months').text($months[future_month]);
});
您也可以通过next
导航执行此操作!只需看看上面的代码,然后尝试下一个!
答案 1 :(得分:1)
是这样的:
$('.history-months').click(function () {
var num = $('.months').attr('month-count');
var future_month = parseFloat(num) - 1;
if(future_month < 1) future_month = 12;
$('.months').attr("month-count", future_month);
$('.months').text($months[future_month]);
});
$('.future-months').click(function () {
var num = $('.months').attr('month-count');
var future_month = parseFloat(num) + 1;
if(future_month > 12) future_month = 1;
$('.months').attr("month-count", future_month);
$('.months').text($months[future_month]);
答案 2 :(得分:0)
谢谢你们......
Dhaval你的解决方案就像一个魅力,但我自己找到了一个简单的解决方案
我只是改变了:
if (num < 2)
要
if (num < 3)
在.history-month