这是我到目前为止的内容,但是甚至没有提示用户,它显示了一条细黑线(我认为是边框)
<html>
<head>
<meta charset = "utf-8">
<title>Dynamically creating a table</title>
</head>
<body>
<table style = "width:100%" cellspacing="5" border="2">
<script type = "text/javascript">
var running = true; //boolean to determine if we should continue running
while(running) {
var i = 0;
var input; //variable that holds the input values
input = window.prompt("Please enter your product number then quantity sold, seperated by a comma. Hit Enter when you are done.");
var split;
var num1;
var num2;
if(input != "") {
split = input.split(',');
num1 = split[0];
num2 = 0;
if(split.length > 1) {
num2 = split[1].trim();
}
}
switch(num1) {
case 1:
// unimportant calculations
break;
case 2:
// unimportant calculations
break;
default:
document.writeln("<tr>");
document.writeln("<td>text</td>");
document.writeln("<td>text</td>");
document.writeln("</tr>");
document.writeln("<tr>");
document.writeln("<td>text1</td>");
document.writeln("<td>text2</td>");
document.writeln("<td>text3</td>");
document.writeln("</tr>");
running = false;
}
}
</script>
</table>
</body>
</html>
我尝试将其包括在头部和正文中,还尝试在脚本中和脚本外部声明table标签。
最佳答案
您的代码中有一些错误,例如:
<table style = "width:100%" cellspacing="5" border="2">
您不能将像表这样的html代码放在头部内,您需要在主体上编写此代码。
var input[i]; //variable that holds the input values
您不能定义包含键的变量。正确的方法是:
var input = [];
其他
switch(num1):
// ... code
正确的方法是:
switch(num1) {
// ... code
}
您可以使用html validator检查html错误,如果您使用的是现代浏览器(如Firefox或Chrome),则可以按f12键访问javascript错误。