HTML和Javascript,window.prompt

时间:2012-06-24 21:36:11

标签: javascript html

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>


2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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>