任何人都可以发现一个明显的错误吗?为什么这个Javascript生成表(showResults
函数)没有显示在我的html?当我有很多文字值引用时,总会发生这种情况......我也很感激我的格式化(空格)上的任何其他评论!Javascript generated table not working
<script type="text/javascript">
//calculates sum of all values within an array
function totalVotes(votes){/
var total=0;
for (i=0;i<votes.length;i++){
total = votes[i] + total;
}
return total;
}
//calculates a percentage, rounded to nearest integer
function calcPercent(item, sum){
return Math.round((item/sum)*100);
}
//tests value of partyType parameter
function createBar(partyType, percent){
switch (partyType){
case D: barText="<td class='dem'></td>";
break;
case R: barText="<td class='rep'></td>";
break;
case I: barText="<td class='ind'></td>";
break;
case G: barText="<td class='green'></td>";
break;
case L: barText="<td class='lib'></td>";
break;
}
for(i=1;i<=percent;i++){
document.write(barText);
}
}
function showResults(race,name,party,votes){
var totalV=function totalVotes(votes);
document.write("<h2>" +race+ "</h2>");
document.write("<table cellspacing='0'>");
document.write("<tr>");
document.write("<th>Candidate</th>");
document.write("<th class='num'> Votes</th>");
document.write("<th class='num'>%</th>");
document.write("</tr>");
for (r=0;r<name.length;r++){
document.write("<tr>");
document.write("<td>"+name[i]+ '(' +party[r]+ ")" +"</td>");
document.write("<td class='num'>" +votes[r]+ "</td>");
var percent=function calPercent(votes[r],totalV)
document.write("<td class='num'>(" +percent[r]+ "%)</td>");
createBar(party[r], percent);
document.write("</tr>");
}
document.write("</table>");
}
很确定'var totalV = function totalVotes(votes);'是无效的Javascript。我认为你的意思是'var totalV = totalVotes(votes);'。 – Turnerj
这也是无效的'for(i = 1; i <= percent; i ++)''通过您的代码在这里''document.write(“+ tt class ='num'>(”+ percent [r] +“ %)“);',我想你的意思是写'for(i = 1; i
Turnerj