HTML和Javascript,window.prompt
对javascript和html类型的东西非常陌生。我只想用用户的输入做一个简单的例子,并将其输出到一个表中。不过,我很难让窗口提示真正出现。我想有一件事很明显我做错了,但我现在没有看到……我在学校上课,但这不是家庭作业,只是我自己做的练习 这是while循环吗?在用户声明之前,我应该如何不断提示用户,有什么建议吗HTML和Javascript,window.prompt,javascript,html,Javascript,Html,对javascript和html类型的东西非常陌生。我只想用用户的输入做一个简单的例子,并将其输出到一个表中。不过,我很难让窗口提示真正出现。我想有一件事很明显我做错了,但我现在没有看到……我在学校上课,但这不是家庭作业,只是我自己做的练习 这是while循环吗?在用户声明之前,我应该如何不断提示用户,有什么建议吗 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>
<!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>
<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>
里程记录
桌子{
宽度:300px;
边界塌陷:塌陷;
背景颜色:浅蓝色;
}
表,td,th{
边框:1px纯黑;
填充:4px;
}
th{
文本对齐:左对齐;
颜色:白色;
背景色:深蓝色
}
奥德罗{
背景色:白色;
}
var milesDriven;
var加仑;
var-mpg;
var不再存在;
文件。书面形式(“”);
书面文件(“行驶里程”);
书面文件(“使用的加仑”);
书面文件(“MPG”);
文件。书面形式(“”);
while(more==true){
milesdrive=window.prompt(“你开了多少英里?”);
gallonsUsed=window.prompt(“您使用了多少加仑?”);
mpg=英里/加仑;
document.writeln(“+milesDriven+”)
加仑/加仑+mpg+);
more=确认(“您还有更多数据要输入吗?”);
}
文件。书面形式(“”);
该方法存在几个问题。您不应使用
document.writeln()
修改文档内容。而是创建元素并将其添加到文档树中。一个好的JavaScript教程应该告诉您如何做到这一点。它还将解释标识符是区分大小写的,因此您不能只在这里和那里写了。标识符中还有其他输入错误。由于未定义的值不等于true
,因此您的循环从未执行过。该方法存在几个问题。您不应使用document.writeln()
修改文档内容。而是创建元素并将其添加到文档树中。一个好的JavaScript教程应该告诉您如何做到这一点。它还将解释标识符是区分大小写的,因此您不能只在这里和那里写了。标识符中还有其他输入错误。而且由于未定义的值不等于true
,因此您的循环永远不会执行。
里程记录
桌子{
宽度:300px;
边界塌陷:塌陷;
背景颜色:浅蓝色;
}
表,td,th{
边框:1px纯黑;
填充:4px;
}
th{
文本对齐:左对齐;
颜色:白色;
背景色:深蓝色
}
奥德罗{
背景色:白色;
}
//var milesDriven;
//var加仑;
//var-mpg;
var=0;
var str=“”;
str+=“行驶里程”;
str+=“使用的加仑数”;
str+=“MPG”;
str+=”;
函数milesDriven(){
返回窗口。提示(“您行驶了多少英里?”);
}
函数gallonsUsed(){
返回窗口。提示(“您使用了多少加仑?”);
}
函数mpg(){
返回行驶里程()/gallonsUsed();
}
函数addComponentsToDOM(){
而(不再
里程记录
桌子{
宽度:300px;
边界塌陷:塌陷;
背景颜色:浅蓝色;
}
表,td,th{
边框:1px纯黑;
填充:4px;
}
th{
文本对齐:左对齐;
颜色:白色;
背景色:深蓝色
}
奥德罗{
背景色:白色;
}
//var milesDriven;
//var加仑;
//var-mpg;
var=0;
var str=“”;
str+=“行驶里程”;
str+=“使用的加仑数”;
str+=“MPG”;
str+=”;
函数milesDriven(){
返回窗口。提示(“您行驶了多少英里?”);
}
函数gallonsUsed(){
返回窗口。提示(“您使用了多少加仑?”);
}
函数mpg(){
返回行驶里程()/gallonsUsed();
}
函数addComponentsToDOM(){
然而(无论如何,谢谢,我认为打字错误是在重新编写脚本和诸如此类的内容之后发生的。我现在已经开始工作了。关于使用document.writeln vs elements:这正是我所想的(过去已经做过了,但很久以前),但实际上在这个类中,我们被告知要使用document.writeln(),愚蠢,对吗?另外,我们正在使用的书是Deitel的第五版Internet和万维网,它是关于js use document.writeln.ArghhHthanks的前几章,我认为拼写错误是在重新编写脚本和诸如此类的内容之后发生的。我现在已经开始工作了。关于使用document.writeln vs elements:这正是我所想的(在过去做过,但很久以前),但在这个类中,我们实际上被告知要使用document.writeln(),愚蠢,对吧?另外,我们正在使用的书是Deitel的第五版Internet和万维网,它是关于js use document.writeln.arghhhhhmy QQ帐户243030330,我来自中国我的QQ帐户243030330,我来自中国