日期:2014-05-16 浏览次数:20426 次
<form> <label for="birthday">生日:</label> <input id="birthday" name="birthday" type="text" value="2002-10-12" /> <input id="button" type="button" value="提交" /> <div> <span>年龄:</span> <span id="result"></span> </div> </form> <script> document.getElementById("button").onclick = function(){ var now, birthday, age; now = new Date(); birthday = new Date(document.getElementById("birthday").value); age = new Date(now.getTime() - birthday.getTime()).getFullYear(); document.getElementById("result").innerHTML = age - 1970; } </script>