<!DOCTYPE html>
<html>
<body>
<p>更改月份字段的月份/年份,然后单击下面的按钮。</p>
年和月:<input type="month" id="myMonth" value="2023-06">
<p>请注意,当您更改月份字段的值时,默认值不受影响。</p>
<button type="button" onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myMonth");
var defaultVal = x.defaultValue;
var currentVal = x.value;
if (defaultVal == currentVal) {
document.getElementById("demo").innerHTML = "默认值和当前值相同:"
+ x.defaultValue + " 和 " + x.value
+ "<br>请更改月份字段的月份或年份以查看差异!";
} else {
document.getElementById("demo").innerHTML = "默认值为:" + defaultVal
+ "<br>新的当前值是:" + currentVal;
}
}
</script>
</body>
</html>