【问题标题】:javascript prompt not showingjavascript提示不显示
【发布时间】:2019-07-14 16:43:56
【问题描述】:

我正在尝试为一个类创建一些代码,提示用户输入三个数字,然后对这些数字进行一些计算,数学是对一个数字进行平方,将数字乘以 PI,然后将它们显示在适当的位置细胞。现在我的 onClick 没有工作,也没有提示给用户。我在那里有 min 和 max 函数,因为它是必需的 这是我的代码:

function promptForNumber(promptString, min, max) {
  Array.prototype.max = function() {
    return Math.max.apply(null, this);
  };

  Array.prototype.min = function() {
    return Math.min.apply(null, this);
  };
}

function populateRow(row) {
  var number = promptForNumber("Enter your number");
  row.cells[0].innerHTML = number;
  row.cells[1].innerHTML = Math.pow(number, 2);
  row.cells[2].innerHTML = (number / Math.PI).toFixed(4);
}

function isNotANumber(NaN) {
  var isNotANumer = promptForAValidNumber("Please enter a 
    valid number ")
  }
table,
th,
tr,
td {
  border-collapse: collapse;
}

table {
  width: 80%;
  margin: 10%;
}

th {
  width: 33%;
  border: 2px solid black;
  justify-content: space-evenly;
  height: 25px;
  background-color: white;
}

td {
  border: 1px solid black;
  padding: 1%;
  text-align: center;
  background-color: greenyellow;
}
<!DOCTYPE html>

<html lang="en">

<head>
  <title>Assignment 2</title>
</head>

<body>
  <table>
    <tr>
      <th>Number</th>
      <th>Squared</th>
      <th>Divided by Pi</th>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </table>
</body>

</html>

【问题讨论】:

  • Right now my onClick is not working 你的 onClick 函数在哪里?
  • 我在复制粘贴代码时忘记添加了,我正在玩弄它并删除了一点。它会出现在每个 tr 标头中,看起来像这样—— onClick="populateRow(this);"

标签: javascript html onclick prompt


【解决方案1】:

这看起来像你提到的一个家庭作业问题,所以我不能给你这个问题的确切解决方案。但是,我现在会指出您的代码有什么问题。

  1. 您提到您的“onClick”不起作用,但此代码中没有任何 onClick 函数。
  2. 在JS中需要使用window.prompt()方法提示用户输入。

您需要创建一个按钮,用户可以按下该按钮来接收警报。在此按钮上添加一个事件侦听器,提示用户输入数字。您可以通过此here 获得帮助。将提示中的数字存储在变量中后,使用该变量执行不同的数学运算,并将这些运算添加到表中。

【讨论】:

  • 是的,我读了一遍然后忘记了。我会把 onClick 放在开头的 tds
  • 对不起,它会放在每个块的 trs 中
【解决方案2】:

您的提示代码中有多余的行,请更正您的代码,如下所示:

function isNotANumber(NaN) {
    var isNotANumer = promptForAValidNumber("Please enter a valid number")
}

另外你必须使用标准的提示方法:
https://www.w3schools.com/jsref/met_win_prompt.asp

【讨论】:

  • 这并没有解决 OP 的问题,也没有解决问题。
【解决方案3】:

事实上你需要添加事件监听器来监听点击事件。 可能是这样的

<html lang="en">
 <head>
    <title>Assignment 2</title>
     <style>
            table, th, tr, td {
                border-collapse: collapse;
            }
            table {
                width: 80%;
                margin: 10%;
            }
            th {
                width: 33%;
                border: 2px solid black;
                justify-content: space-evenly;
                height: 25px;
                background-color: white;
            }
            td {
                border: 1px solid black;
                padding: 1%;
                text-align: center;
                background-color: greenyellow;
            }
     </style>
    </head>
 <body>
   <table>
    <tr>
        <th>Number</th>
        <th>Squared</th>
        <th>Divided by Pi</th>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td></td>
    </tr>
  </table>
  <script>
function promptForNumber(promptString, min, max) {
    Array.prototype.max = function() {
        return Math.max.apply(null, this);
    };

    Array.prototype.min = function() {
        return Math.min.apply(null, this);
    };
}

function populateRow(row) {
    var number = window.prompt("Enter your number");
    var cell = row.getElementsByTagName("td");
    cell[0].innerHTML = number;
    cell[1].innerHTML = Math.pow(number, 2);
    cell[2].innerHTML = (number / Math.PI).toFixed(4);

}

function isNotANumber(NaN) {
    var isNotANumer = promptForAValidNumber("Please enter a valid number")
}

var table = document.getElementsByTagName("table")[0];
var rows = table.getElementsByTagName("tr");
console.log('rows', rows);
for (let i = 0; i < rows.length; i++) {
    let currentRow = table.rows[i];
    currentRow.addEventListener("click", function() {

        populateRow(currentRow);

    })


};


 </script>
 </body>
 </html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-05
    • 2011-04-06
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多