【问题标题】:Javascript generated table not workingJavascript生成的表不起作用
【发布时间】:2013-06-15 22:30:02
【问题描述】:

谁能发现一个明显的错误?为什么这个 Javascript 生成的表(showResults 函数)没有出现在我的 html 中?当我有很多文字值要引用时,总是会发生这种情况......我也会感谢我的格式(空白)上的任何其他 cmets!

   <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);
  • 这也是无效的for(i=1;i&lt;=percent;i++),因为在document.write("&lt;td class='num'&gt;(" +percent[r]+ "%)&lt;/td&gt;"); 中返回您的代码,我认为您的意思是写for(i=1;i&lt;percent.length;i++)。对于你的 for 循环还有一点,在声明你迭代的变量时最好使用var,以便在循环之后处理它。

标签: javascript html arrays for-loop html-table


【解决方案1】:

我认为你需要在与字符串比较时给出引号,Hence D is not same as "D".

  switch (partyType){
            case "D":  barText="<td class='dem'></td>";
            break;
            case "R":  barText="<td class='rep'></td>";
            .......

正如其他人所建议的,在调用函数时删除函数关键字。

var totalV = function totalVotes(votes);

应该是

  var totalV = totalVotes(votes);

【讨论】:

  • 谢谢!我以为我尽可能正式,但我没有意识到这是不正确的!
【解决方案2】:

1) 在您的案例陈述中使用引号:case "D"case "R"、...

2) 使用innerHTML 方法代替document.write。仅在连接字符串中使用一次,而不是在 for 循环内。

3) var totalV = function totalVotes(votes); 会报错:也许你想写var totalV = totalVotes(votes);

4) barText 没有在任何地方定义(先用var 声明)


如果您也对通用建议感兴趣,则可以通过对象查找来避免整个切换

function createBar(partyType, percent){   

    var cells     = "",
        cellclass = { 
          "D": "dem", "R": "rem", "I": "ind", "G": "green", "L": "lib" 
        }[partyType];


    for(var i = 1; i <= percent.length; i++){    
        cells += "<td class='" +  cellclass + "'></td>"
    }

    <yourtablerow>.innerHTML = cells; // <yourtablerow> is a reference to a table row.
}

【讨论】:

  • 我喜欢你对createBar 的建议,非常聪明。不过,他的代码有一件事是 percent 是一个传入的数组,因此对于您的 for 循环,它需要是 percent.length。 :)
  • 是的巧妙建议(在这一点上完全超出了我的能力——我显然才刚刚开始,哈哈)。千禧年,法布里齐奥!
【解决方案3】:

您的代码存在很多问题:

  1. totalVotes 函数的左大括号后的随机斜杠

  2. 用你的 for 循环声明一个没有 var 的变量(这不会破坏你的代码,但这样做不是一个好主意)

  3. 正如其他人所说,switch语句的使用不正确(非数字字符需要用引号引起来)

  4. 正如 Fabrizio Calderan 所说,barText 设置不正确。它工作,但这是非常糟糕的做法。在 switch 之前先声明它。

  5. createBar 中的 for 循环不会根据您传递给它的内容起作用。您在showResults 内的for 循环中将数组作为percent 的值传递,然后通过将其与数字进行比较来将其视为数字。你希望 percent.length 在 for 循环中。

  6. 访问函数的方法不正确。 var totalV=function totalVotes(votes); 无效且无法使用。或者使用var totalV=totalVotes(votes);,或者如果你不想使用它,var totalV=totalVotes.call(null, votes);(虽然你不需要在你的代码中使用.call属性)

  7. showResults 函数中的 for 循环内的另一个错误。 var percent=function calPercent(votes[r],totalV) 的问题是我的第 6 点,即错误地访问函数以及由于拼写错误而实际上不存在该函数。很确定你的意思是var percent=calcPercent(votes[r],totalV)

  8. 我不是 document.write 方法的粉丝,并且很长时间没有使用它。由于它的工作方式/写入位置,我个人不惜一切代价避免它。此外,正如 Fabrizio 所建议的,innerHTML 是一个更好的选择。

总体而言,代码很干净并且大部分都可以运行,但我觉得您甚至没有尝试在启用调试器的情况下运行代码。 Firefox 和 Chrome 都有一个不错的原生调试器,并且会发现这些错误。

话虽如此,我已经解决了这些问题的代码。仍然可以进行一些其他更改以使代码更好/更小/更简单,但我认为现在就可以了。

<script type="text/javascript"> 
    //calculates sum of all values within an array
    function totalVotes(votes)
    {
        var total=0;
        for (var 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)
    {
        var barText = null;
        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;
        }
        var result = "";
        for(var i=1;i<percent.length;i++)
        {
            result += barText;
        }
        return result;
    }
    function showResults(race,name,party,votes)
    {
        var totalV=totalVotes(votes);
        var result = "";

        result += "<h2>" +race+ "</h2>";
        result += "<table cellspacing='0'>";
        result += "<tr>";
        result += "<th>Candidate</th>";
        result += "<th class='num'> Votes</th>";
        result += "<th class='num'>%</th>";
        result += "</tr>";

        var percent = 0;
        for (var r=0; r < name.length; r++){
            result += "<tr>";
            result += "<td>"+name[i]+ '(' +party[r]+ ")" +"</td>";
            result += "<td class='num'>" +votes[r]+ "</td>";

            percent = calcPercent(votes[r],totalV)
            result += "<td class='num'>(" +percent[r]+ "%)</td>";
            result += createBar(party[r], percent);
            result += "</tr>";
        }
        result += "</table>";

        document.body.innerHTML = result; // or document.write(result) if you prefer
    }
</script>

【讨论】:

  • 谢谢你,特纳吉!您的反馈非常有帮助!你是对的......我没有像我应该的那样利用调试器,这会适得其反。再次感谢我的老师们!我会学习的!
猜你喜欢
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 2017-05-22
相关资源
最近更新 更多