项目中用到的一些简单JS小知识,动态创建年月下拉框:
JS代码:
var date = new Date();
var y = date.getFullYear();
var m = date.getMonth() + 1;
for (i = 0; i < 10; i++) {
var oP = document.createElement("option");
var oText = document.createTextNode(y);
oP.appendChild(oText);
oP.setAttribute("value", y);
document.getElementById('year').appendChild(oP);
y = y - 1;
};
var j = 1;
for (i = 1; i < 13; i++) {
var month = document.createElement("option");
var monthText = document.createTextNode(j);
month.appendChild(monthText);
month.setAttribute("value", j);
if (j == m) {
month.setAttribute("selected", "selected");
}
;
document.getElementById('month').appendChild(month);
j = j + 1;
};
JSP页面:
<table >
<tr>
<td>
年份:
<select id="year">
</select>
</td>
<td>
月份:
<select id="month">
</select>
</td>
<td>
<input type="button" value="确定" onclick="show()" />
</td>
</tr>
</table>
效果图:
来源:https://www.cnblogs.com/rason2008/archive/2012/03/23/2414036.html