为什么Inspect元素中的错误显示函数myNUM()没有定义?如何定义函数

Why does the error in the Inspect Element show that function myNUM() is not defined ? How to define a function?

本文关键字:函数 定义 何定义 myNUM 元素 Inspect 显示 错误 为什么      更新时间:2024-03-31

这段代码是关于数字验证的。我今天尝试了这段代码,发现执行起来很困难。错误表明函数未定义?为什么没有定义函数?我们是如何知道函数是被定义的?

<!DOCTYPE html>
<body>
<h1>JS can validate number</h1> 
<p><tt>Please enter any number from 1 to 10</tt></p>
<input id="Number">
<button type="button" onclick="myNUM()">Submit</button>
<p id="demo"></p>
<script>
function myNUM()
{
var x,text;
x=document.getElementById("Number").value;
if
(isNaN(x)||x<1||x>10)
text="Input is not valid";
}
else 
{
text="Input is valid";
}
document.getElementById("demo").innerHTML=text;
}
</script>
</html>

u在if(isNaN(x)||x<1||x>10)之后缺少一个{。确保你没有;我破坏了javascript。使用可以验证HTML的标准编辑器。

<!DOCTYPE html>
<head>
<title>My Html Page</title>
<script type="text/javascript">
function myNUM()
{
var x,text;
x=document.getElementById("Number").value;
if(isNaN(x)||x<1||x>10)
{
text="Input is not valid";
}
else 
{
text="Input is valid";
}
document.getElementById("demo").innerHTML=text;
}
</script>
</head>
<body>
<h1>JS can validate number</h1> 
<p><tt>Please enter any number from 1 to 10</tt></p>
<input id="Number">
<button type="button" onclick="myNUM()">Submit</button>
<p id="demo"></p>
</body>
</html>

您没有在"if"语句后启动"{"。

    if
    (isNaN(x)||x<1||x>10)
   {
    text="Input is not valid";
   }