我有以下两个功能,我试图让用户输入一年,然后答案将产生答案。但是,我如何告诉函数识别字符串,然后如果答案不是 MM/DD/YYYY 形式,运行函数 wholePigLatin?基本上,我如何附加相同的按钮来运行这两个函数取决于什么用户投入。任何帮助将不胜感激。谢谢。
function isLeaper() {
var image1 = document.getElementById('yes');
var image2 = document.getElementById('no');
var year = document.getElementById("isLeaper").value;
var arr = year.split('/');
var splitYear = arr[arr.length - 1];
// 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) {
$('#myDiv').html(image2).fadeIn(500).delay(1000).fadeOut(500);
// alert(year + 'is not a leap year. Sorry!');
return "false";
}
if (parseInt(splitYear) % 400 == 0) {
$('#myDiv').html(image1).fadeIn(500).delay(1000).fadeOut(500);
//alert(splitYear + ' is a leap year. Hooray! ');
return "true";
}
}
if (parseInt(splitYear) % 100 != 0) {
$('#myDiv').html(image1).fadeIn(500).delay(1000).fadeOut(500);
//alert(splitYear + ' is a leap year. Hooray! ');
return "true";
}
}
if ((parseInt(splitYear) % 4) != 0) {
$('#myDiv').html(image2).fadeIn(500).delay(1000).fadeOut(500);
//alert(splitYear + ' is not a leap year. Sorry! ');
return "false";
}
}
if ((parseInt(year) % 4) != 0) {
$('#myDiv').html(image2).fadeIn(500).delay(1000).fadeOut(500);
return "false";
}
我的第二个功能如下:
function wholePigLatin() {
var thingWeCase = document.getElementById("pigLatin").value;
thingWeCase = thingWeCase.toLowerCase();
var newWord = (thingWeCase.charAt(0));
if (newWord.search(/[aeiou]/) > -1) {
alert(thingWeCase + 'way')
}
else {
var newWord2 = thingWeCase.substring(1, thingWeCase.length) + newWord + 'ay';
alert(newWord2)
}
}
这是我的按钮。
<input type="text" id="isLeaper" value="MM/DD/YYYY">
<input type="button" value="Is Leap Year?" onclick="isLeaper();">