0

对 javascript 和 html 类型的东西非常陌生。我只想使用来自用户的输入并将其输出到表格中来做一个简单的示例。我无法让窗口提示实际出现。我想有一些非常明显的事情表明我做错了,但我目前没有看到它......我正在学校上课,但这不是家庭作业,只是我自己做的练习。

这与while循环有关吗?关于我应该如何继续提示用户的任何建议,直到他们另有说明?

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Mileage Record</title>
<style type = "text/css">
    table {
        width:300px;
        border-collapse:collapse;
        background-color:lightblue;
        }
    table, td, th {
        border: 1px solid black;
        padding: 4px;
        }
    th {
        text-align: left;
        color: white;
        background-color: darkblue
        }
    tr.oddrow {
        background-color: white;
        }
</style>

<script>
    var milesDriven;
    var gallonsUsed;
    var mpg;
    var anyMore;

    document.writeln("<table>");
    document.writeln("<thead><tr><th>Miles Driven</th>");
    document.writeln("<th>Gallons Used</th>");
    document.writeln("<th>MPG</th>");
    document.writeln("</tr></thead><tbody>");

    while (anyMore == true) {
        milesDriven = window.prompt("How many miles did you drive?");
        gallonsUsed = window.prompt("How many gallons did you use?");
        mpg = milesDrive/gallonsUsed;
        document.writln("<tr><td>" + milesDriven + "</td><td>" + 
            gallonsUsed + "</td><td>" + mpg + "</td></tr>");

        anymore = confirm("Do you have any more data to input?");
    }
    document.writeln("</tbody></table>");

</script>


4

2 回答 2

3

该方法存在几个问题。您不应该使用document.writeln()来修改文档内容。相反,创建元素并将它们添加到文档树中。一个好的 JavaScript 教程应该会告诉你如何做到这一点。它还将解释标识符是区分大小写的,所以你不能在anyMore这里和anymore那里写。标识符中还有其他拼写错误。而且由于未定义的值不等于true,因此您的循环永远不会执行。

于 2012-06-24T21:47:05.753 回答
0
<pre>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Mileage Record</title>
<style type = "text/css">
    table {
        width:300px;
        border-collapse:collapse;
        background-color:lightblue;
        }
    table, td, th {
        border: 1px solid black;
        padding: 4px;
        }
    th {
        text-align: left;
        color: white;
        background-color: darkblue
        }
    tr.oddrow {
        background-color: white;
        }
</style>

<script>
   // var milesDriven;
  //  var gallonsUsed;
  //  var mpg;
    var anyMore=0;
    var str="<table>";
        str+="<thead><tr><th>Miles Driven</th>";
        str+="<th>Gallons Used</th>";
        str+="<th>MPG</th>";
        str+="</tr></thead><tbody>";


function milesDriven(){
  return window.prompt("How many miles did you drive?");
}

function gallonsUsed(){
  return window.prompt("How many gallons did you use?");
}

function mpg(){
 return milesDriven()/gallonsUsed();
}


function addComponentsToDOM(){
    while (anyMore<3) { //not 0 is true or not undefined is true or not null is true 
    str+="<tr><td>" + milesDriven() + "</td><td>" + 
            gallonsUsed() + "</td><td>" + mpg() + "</td></tr>"
      //  anymore = confirm("Do you have any more data to input?");
      anyMore++;
    }

    str+="</tbody></table>";
    //document.writeln(); i suggest you not use this method add component to DOM tree
    //because  this method first empty DOM tree content
     document.body.innerHTML=str;
 }


//why use this event,because window after  loaded document.body not null
 window.onload=addComponentsToDOM;


</script>
</pre>
于 2012-06-25T03:22:02.807 回答