Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery切换1月或12月的年份_Jquery_Toggle - Fatal编程技术网

使用jQuery切换1月或12月的年份

使用jQuery切换1月或12月的年份,jquery,toggle,Jquery,Toggle,我们已经编写了一个代码来切换月份和年份,就像您在右下角单击windows时钟时看到的那样。我只想实现上面用红色突出显示的部分。下面没有日历 我已经取得了所有的成就,但也有一些小问题。当您转到未来月份时,它会在12月份更改年份。它应该在1月份改变年份。你回去的时候也一样。你可以在这里看到真实的例子 以下是我的jQuery代码: var $months = [ "", "January", "February", "March", "April",

我们已经编写了一个代码来切换月份和年份,就像您在右下角单击windows时钟时看到的那样。我只想实现上面用红色突出显示的部分。下面没有日历

我已经取得了所有的成就,但也有一些小问题。当您转到未来月份时,它会在12月份更改年份。它应该在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 month_num = $('.months').attr('month-count');
    var year_num = $('.years').text();
    var history_month = parseFloat(month_num) - 1;
    $('.months').attr("month-count", history_month);
    $('.months').text($months[history_month]);
    if (month_num < 3) {
        $('.months').attr("month-count", 13);
        var history_year = parseFloat(year_num) - 1;
        $('.years').text(history_year);
    }
});
$('.future-months').click(function () {
    var month_num = $('.months').attr('month-count');
    var year_num = $('.years').text();
    var future_month = parseFloat(month_num) + 1;
    $('.months').attr("month-count", future_month);
    $('.months').text($months[future_month]);
    if (month_num > 10) {
        $('.months').attr("month-count", 0);
        var history_year = parseFloat(year_num) + 1;
        $('.years').text(history_year);
    }
});

$('.months').text($months[$m]).attr('month-count', $m);
$('.years').text($y);
var$months=[
"",
“一月”,
“二月”,
“三月”,
“四月”,
“五月”,
“六月”,
“七月”,
“八月”,
“9月”,
“十月”,
“11月”,
“12月”];
var d=新日期();
var$m=d.getMonth()+1;
变量$y=d.getFullYear();
$('.history months')。单击(函数(){
var month_num=$('.months').attr('month-count');
var year_num=$('.years').text();
var history\u month=parseFloat(month\u num)-1;
$('.months').attr(“月数”,历史月);
$('.months').text($months[history_month]);
如果(月数<3){
$('.months').attr(“月计数”,13);
var history\u year=parseFloat(year\u num)-1;
元(“.years”)。文本(历史\年);
}
});
$(“.未来月份”)。单击(函数(){
var month_num=$('.months').attr('month-count');
var year_num=$('.years').text();
var future\u month=parseFloat(month\u num)+1;
$('.months').attr(“月份计数”,未来月份);
$('.months').text($months[未来月份]);
如果(月数>10){
$('.months').attr(“月计数”,0);
var history_year=parseFloat(year_num)+1;
元(“.years”)。文本(历史\年);
}
});
$('.months').text($months[$m]).attr($month-count',$m);
元(“.years”)。文本($y);
以下是HTML:

<a href="#" class="history-months">&LT;</a>

<span class="months-years">
    <span class="months"></span>
    <span class="years"></span>
</span>

<a href="#" class="future-months">&GT;</a>

有什么想法吗

谢谢


Omer

Ok我自己找到了解决方案:p

我想我也应该和全世界分享:)

只需稍微编辑一下我的条件:

$('.future-months').click(function() {
    var month_num = $('.months').attr('month-count');
    var year_num = $('.years').text();
    var future_month = parseFloat(month_num) + 1;
    if (future_month > 12) {
         future_month = 1;
         year_num = parseFloat(year_num) + 1;
    }
    $('.months').attr("month-count", future_month);
    $('.months').text($months[future_month]);
    $('.years').text(year_num);
});

$('.history-months').click(function() {
    var month_num = $('.months').attr('month-count');
    var year_num = $('.years').text();
    var history_month = parseFloat(month_num) - 1;
    if (history_month < 1) {
         history_month = 12;
         year_num = parseFloat(year_num) - 1;
    }
    $('.months').attr("month-count", history_month);
    $('.months').text($months[history_month]);
    $('.years').text(year_num);
});
$('.future months')。单击(函数(){
var month_num=$('.months').attr('month-count');
var year_num=$('.years').text();
var future\u month=parseFloat(month\u num)+1;
如果(未来月份>12){
未来月份=1;
年数=年数+1;
}
$('.months').attr(“月份计数”,未来月份);
$('.months').text($months[未来月份]);
$('.years')。文本(年数);
});
$('.history months')。单击(函数(){
var month_num=$('.months').attr('month-count');
var year_num=$('.years').text();
var history\u month=parseFloat(month\u num)-1;
如果(历史月<1){
历史月=12;
年数=parseFloat(年数)-1;
}
$('.months').attr(“月数”,历史月);
$('.months').text($months[history_month]);
$('.years')。文本(年数);
});
希望对其他人有所帮助:)

奥马尔