Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我是javascript初学者,我会';无法在浏览器上运行此代码_Javascript_Getelementbyid - Fatal编程技术网

I';我是javascript初学者,我会';无法在浏览器上运行此代码

I';我是javascript初学者,我会';无法在浏览器上运行此代码,javascript,getelementbyid,Javascript,Getelementbyid,我只想让getElementById正常工作,但我可以,我使用chrome时会出现以下错误: Uncaught ReferenceError: calculate is not defined 以下是html: <html> <body> <table> <tr><td>Input the test variable:</td> <td><input id="x" onchange="calcula

我只想让getElementById正常工作,但我可以,我使用chrome时会出现以下错误:

Uncaught ReferenceError: calculate is not defined 
以下是html:

<html>
<body>


<table>
<tr><td>Input the test variable:</td>
<td><input id="x" onchange="calculate();"></td></tr>

<tr><th>Calculate the number</th>
<td><button onclick="calculate();">Calculate</button></td></tr>
</table>


<script src="test1javascript.js"></script>

</body>
</html>

您应该首先定义函数,然后使用onload:

function calculate(){
   var x=document.getElementById("x");
   if(x==5){
      alert(x);
   }
}
window.onload = calculate;

您必须自行定义
计算

function calculate() {
    // Added .value because you want the content and not the object itself
    var x = document.getElementById("x").value;
    if (x == 5) {
        alert(x);
    }
}
然后将其添加到
窗口。onload
事件:

window.addEventListener('load', calculate);

您正在
window.onload
上指定函数。此外,您还将同一函数绑定到
input onchange
按钮onclick
。而是按如下方式声明函数,并仅将函数绑定到单击按钮时的
按钮。还要注意
document.getElementById(“x”)

另外,最好在
中包含脚本文件,并指定
类型
属性:

<html>
  <head>
    <script src="test1javascript.js" type="text/javascript"></script>
  </head>
  <body>
    <table>
       <tr><td>Input the test variable:</td>
       <td><input id="x" onchange="calculate();"></td></tr>
       <tr><th>Calculate the number</th>
       <td><button onclick="calculate();">Calculate</button></td></tr>
    </table>
  </body>
</html>

输入测试变量:
算数
算计

document.getElementById
返回对元素(顾名思义是
getElement
,但不是其
)及其所有属性的引用。在上面的例子中,它是一个按钮。您需要使用其所需的属性来访问所需的属性


阅读更多信息。

为什么您没有指定输入类型,比如:
——我建议您使用:
onkeyup
onkeydown
onkeypress
事件来观察文本字段中的更改。删除窗口。onload=来自javascript。@zlomerovic-
type=“text”
是默认设置。关于键事件处理程序的使用,您还需要一个模糊或更改处理程序来允许非键盘更新(剪切/粘贴/拖放)。我想您可能可以省略
onload
部分。非常感谢大家。我声明type=“text”,尽管我认为这是不必要的。然后我将window.onload放在代码的底部,删除了错误,但我仍然没有删除警报。我认为,最终为getElementById添加.value是它工作的原因。谢谢大家。@user2606257问题解决了吗?您可以将其标记为答案。还有,为什么document.getElementById(“x”)后面需要.value?
document.getElementById
返回对元素的引用(顾名思义是
getElement
,但不是它的值)及其所有属性。您需要使用其所需的属性来访问所需的属性。我修改了答案。检查一下。
function calculate(){
  var x=document.getElementById("x").value;
  if(x==5){
    alert(x);
  }
}
<html>
  <head>
    <script src="test1javascript.js" type="text/javascript"></script>
  </head>
  <body>
    <table>
       <tr><td>Input the test variable:</td>
       <td><input id="x" onchange="calculate();"></td></tr>
       <tr><th>Calculate the number</th>
       <td><button onclick="calculate();">Calculate</button></td></tr>
    </table>
  </body>
</html>