JS Dynamically Generate Year and Month Instance Code
- 2021-07-16 01:28:48
- OfStack
Let's look at the year when js was dynamically generated. The specific code is as follows:
function makeYear() {
var year = document.getElementById("year");
var my = new Date();
var endYear = my.getFullYear();// Get the current year
for (var i = 1900; i <= endYear; i++) {
year.options.add(new Option(i, i + ' Year '));
}
}
Let's take a look at js Dynamic Generation Month
function makeMonth() {
var month = document.getElementById("month");
for (var i = 1; i <= 12; i++) {
/*
* if (i < 10) {
option.values='0'+i;
option.innerText='0'+i;
* } else
* {
* option.values=i;
* option.innerText=i;
* }
* month.appendChiled(option);
*
*/
if (i < 10) {
month.options.add(new Option('0' + i, '0' + i));
} else {
month.options.add(new Option(i, i));
}
}
}
The two if can be interchanged, both in the same way.