0

我想要做的是让我的闰年函数只返回当用户以 MM/DD/YYYY 形式输入日期时的年份值。现在,我只使用一年,但我希望该功能要求用户以该表格输入日期并计算它是否是闰年。如何修改我当前的代码来做到这一点。我已经用 split 方法声明了一个新变量。谢谢!

function isLeaper() {
            var year = document.getElementById("isLeaper").value;
            var splitYear = year.split ('/');
            // 1. If the year is divisible by 4, but not 100.
            if ((parseInt(splitYear) % 4) == 0) {
                if (parseInt(splitYear) % 100 == 0) {
                    if (parseInt(splitYear) % 400 != 0) {
                        alert(year + 'is not a leap year. Sorry!');
                        return "false";
                    }
                    if (parseInt(splitYear) % 400 == 0) {
                        alert(year + 'is a leap year. Hooray!');
                        return "true";
                    }
                }
                if (parseInt(splitYear) % 100 != 0) {
                    alert(year + 'is a leap year. Hooray!');
                        return "true";
                }
            }
            if ((parseInt(splitYear) % 4) != 0) {
                alert(year + 'is not a leap year. Sorry!');
                        return "false";
            }
        }
4

1 回答 1

0

改变

var year = document.getElementById("isLeaper").value;
var splitYear = year.split ('/');

var year = document.getElementById("isLeaper").value;
var arr = year.split('/');
var splitYear = arr[arr.length - 1];

year如果变量的格式为 MM/DD/YYYY 和 YYYY,这将获得年份。

你可以试试这个:

$('#yourContainerID').html(year + 'is not a leap year. Sorry!').fadeIn('normal',function(){
    $(this).fadeOut();
});

有关详细信息,请参阅.fadeIn().fadeOut()

于 2013-03-18T03:53:26.450 回答