JS求和问题
<div>
<ul>
<li>20</li>
<li>30</li>
<li>50</li>
</ul>
<ul>
<li>总计:</li>
</ul>
</div>
20+30+50=? 多少个数字相加不确定
希望能给出实例,俺很菜
------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function getSum(){
var num = document.getElementById("num");
var li = num.getElementsByTagName("li");
var sum=0;
for(var i = 0;i<li.length;i++){
var li_value = li[i].textContent;
sum+=parseInt(li_value);
}
document.getElementById("sum").textContent=sum;
}
</script>
</head>
<body onload="getSum()">
<div>
<ul id = "num">
<li>10</li>
<li>20</li>
<li>30</li>
</ul>
<ul ><li id = "sum"></li></ul>
</div>
</body>
</html>