使用Javascript在提示之间创建中断

时间:2017-01-31 20:58:30

标签: javascript html break prompt multiplication

所以基本上我正在努力使网页不断提示你输入一个数字,直到你输入 -1 。然后,它将停止并发布您输入的所有先前数字的乘法表。

这是我到目前为止所做的,我真的不确定从这里开始。

<script>
    var num = prompt("Enter Number", "0")

    var num = parseInt(num);
    var i = 0;

    document.write('<table ="1">');
    for (i = 1; i < 13; i++) {
        document.write("<tr><td>" + num + " x " + i + " = " + num * i + "</td></tr>");
    }

    document.write("</table>");

    var num = prompt("Enter Number", "0")

    document.write('<table ="1">');
    for (i = 1; i < 13; i++) {
        document.write("<tr><td>" + num + " x " + i + " = " + num * i + "</td></tr>");
    }

    document.write("</table>");

    var num = prompt("Enter Number", "0")

    document.write('<table ="1">');
    for (i = 1; i < 13; i++) {
        document.write("<tr><td>" + num + " x " + i + " = " + num * i + "</td></tr>");
    }

    document.write("</table>");


</script>

2 个答案:

答案 0 :(得分:2)

使用do...while循环。下面的代码还使用Document.createElement()创建DOM元素,document.createTextNode()Node.appendchild()将它们添加到容器元素中,而不是使用document.write()

document.addEventListener('DOMContentLoaded', function() {
  var num;
  var container = document.getElementById('container');
  do {
    num = prompt("Enter Number", "0");
    if (num > 0) {
      var table = document.createElement('table');
      for (i = 1; i < 13; i++) {
        var row = document.createElement('tr');
        var cell = document.createElement('td');
        cell.appendChild(document.createTextNode(num + " x " + i + " = " + num * i));
        row.appendChild(cell);
        table.appendChild(row);
      }
      container.appendChild(table);
    }
  } while (num > 0);
});
td {
  border: 2px solid #f00;
  color: #00f;
}
<div id="container">
</div>

答案 1 :(得分:1)

与上一篇文章类似,只是简单地评论说已经过去了。是的,避免使用document.write - 这在2000年代早期的书籍中很棒,但它已经过时,在很多情况下会破坏。我还假设,当你说乘法表时,你想迭代输入数字并为该数字创建一个完整的表。因此,两个内部循环:一个用于输入数字,一个用于最多十二次迭代。

祝你好运!

&#13;
&#13;
var num = 0, total = 0;

num = parseInt(prompt("Enter Number or -1 to end", 0));
// Keep doing this loop until -1 is entered.
while (num != -1){
// Create a table element, but don't add it yet.
var myTable = document.createElement("table");
  // This line loops to the prompted number...
  for (var i=0; i<=num; i++){
    // ... and creates each ROW of the table  
    var myTr = document.createElement("tr");
    // this line loops for 12 iterations..
    for (var j=0; j<13; j++){
      // and creates and adds each TD element to the current row
      var myTd = document.createElement("td");
      myTd.innerHTML = j+" * "+i+" = "+j*i;
      myTr.appendChild(myTd);
    }
    // Having added all the TDs to the row, we add the row to the table
    myTable.appendChild(myTr);
  }
  // Having added all the rows to the table, we can add the table to the document!
document.body.appendChild(myTable);
// And now ask for another number to make another table, either starting
//   the loop again or breaking out.
num = parseInt(prompt("Enter Number or -1 to end", "0"));
}
&#13;
table {
  border: 1px dotted red;
  font-family: sans-serif;
  font-size: 9px;
}
&#13;
&#13;
&#13;