<script>
function Upd(){//修改
var namevalue = document.getElementById("_name").value
var agevalue = document.getElementById("_age").value
var list = document.getElementById("div1").getElementsByTagName("li")
for(var i=1i<list.lengthi++){
var li=list[i]
var spans = li.getElementsByTagName("span")
if(spans.length>1){ //说明有两个li span
if(spans[0].innerHTML==namevalue){
spans[1].innerHTML=agevalue
}
}
}
}
function Add(){//添加
var namevalue = document.getElementById("_name").value
var agevalue = document.getElementById("_age").value
var str="<li><span>"+namevalue+"</span><span>"+agevalue+"</span></li>"
document.getElementById("div1").innerHTML+=str
var list = document.getElementById("div1").getElementsByTagName("li")
document.getElementById("numpeople").innerHTML = list.length-1
}
</script>
<form>
姓名<input id="_name" name="_name" />
年龄<input id="_age" name="_age" />
<input type="button" name="btnUpd" value="修改" onclick="Upd()" />
<input type="button" name="btnAdd" value="添加" onclick="Add()" />
</form>
<ul id="div1">
<li><span>姓名</span><span>年龄</span></li>
<li><span id="name1">johnson</span><span id="age1">21</span></li>
<li><span id="name2">san</span><span id="age1">9</span></li>
<li><span id="name3">jim</span><span id="age1">19</span></li>
</ul>
<div>统计<span id="numpeople">3</span>人</div>
通过函数代码如下:function computeAge(startDate) {
// 获得今天的时间
var date = new Date()
startDate = new Date(startDate)
var newDate = date.getTime() - startDate.getTime()
// 向下取整 例如 10岁 20天 会计算成 10岁
// 如果要向上取整 计算成11岁,把floor替换成 ceil
return Math.floor(newDate / 1000 / 60 / 60 / 24 / 365)
}
alert(computeAge('1989-02-19'))
就可以了。