这是我到目前为止所拥有的,但它甚至没有提示用户,它显示一条细黑线(我认为是边框)
<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 style = "width:100%" cellspacing="5" border="2">
head里面不能放table之类的html代码,需要写在body上。
var input[i]; //variable that holds the input values
您不能定义包含键的变量。正确的做法是:
var input = [];
其他
switch(num1):
// ... code
正确的做法是:
switch(num1) {
// ... code
}
您可以使用 html validator 检查您的 html 错误如果您使用的是 Firefox 或 Chrome 等现代浏览器,则可以按 f12 访问 JavaScript 错误。
关于javascript - 使用Javascript提示动态创建表(不起作用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688600/