[英]why I'm getting function is not defined?
My html code is:-我的 html 代码是:-
com_hrd.html com_hrd.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="com_hrd.css" />
<title>GUESS</title>
<script type="text/javascript" src="com_hrd.js"></script>
</head>
<body>
<h1 id="a1">GUESS</h1>
<h1 id="a2">_Hard</h1>
<h2 id="a3">Hello <span id="res1"></span></h2>
<div class="hr2"></div>
<button type="submit" id="f1" onclick="msg()">
Generate
</button>
<div id="res5"></div>
<div id="res6"></div>
<div id="res"></div>
<h2 id="a3_1">Enter Your Lucky Number></h2>
<h2 id="a3_2">To Start Game:=></h2>
<div id="snackbar">
Number generated...Enter the Number
</div>
<input type="number" id="a4" />
<button
type="submit"
id="a5"
onclick="cal(document.getElementById('a4').value)"
>
Check
</button>
</body>
</html>
com_hrd.js com_hrd.js
document.getElementById("res1").innerHTML = localStorage.getItem("textvalue3");
function generate() {
var N = Math.floor(Math.random() * 100) + 1;
return N;
}
function msg() {
var x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000);
}
var Num = generate();
function cal(a) {
N1 = a;
// N1 = parseInt(document.getElementById("a4").value);
if (N1 == Num) {
document.getElementById("res").innerHTML = "BINGO!!! You Won!! \n Reload the Page \n To play Again";
break;
}
else {
l1 = [];
a1 = N1.toString();
for (var i = 0, len = a1.length; i < len; i += 1) {
l1.push(+a1.charAt(i));
}
l2 = [];
a2 = Num.toString();
for (var j = 0, len = a2.length; j < len; j += 1) {
l2.push(+a2.charAt(j));
}
if (l2.length == 1) {
if (l1.length == 2) {
if (l1[0] == l2[0] & l1[1] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[1] == l2[0] & l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] == l2[0] & l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l2.length == 2 & l1.length == l2.length) {
if (l1[0] == l2[0] | l1[1] == l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] == l2[1] & l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 2\nPosition Matched: 0";
}
else if (l1[0] == l2[1] | l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l1.length == 2 & l1.length > l2.length) {
if (l1[0] == l2[0] & l1[1] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[1] == l2[0] & l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
}
else if (l2.length == 2 & l1.length < l2.length) {
if (l1[0] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[0] == l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] != l2[0] & l1[0] != l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
}
}
When i am clicking on Check button , cal() function must be called.当我单击检查按钮时,必须调用cal() function。 But it is showing the following error:但它显示以下错误:
Uncaught ReferenceError: cal is not defined
at HTMLButtonElement.onclick (com_hrd.html:36)
I'm unable get the solution.我无法得到解决方案。 Actually that button should work.实际上那个按钮应该可以工作。 And i should be able to see the sentences i have given like, "Number position".而且我应该能够看到我给出的句子,例如“数字位置”。 But dont know why it is not working.但不知道为什么它不起作用。
write return
rather than break
in cal()
function.写return
而不是break
cal()
function。 It is illegal used.它是非法使用的。
if (N1 == Num) {
document.getElementById("res").innerHTML = "BINGO!!! You Won!! \n Reload the Page \n To play Again";
return;
}
You have an illegal break statement within that function, but that doesn't explain the error you're getting.您在 function 中有一个非法的 break 语句,但这并不能解释您遇到的错误。 something else must be wrong with your environment.您的环境肯定有其他问题。 I'd check to make sure your JS file is actually in the location referenced by your HTML.我会检查以确保您的 JS 文件实际上位于 HTML 引用的位置。 Working fiddle for reference (with the illegal break fixed): https://jsfiddle.net/keegangbrown/deapmrjo/工作小提琴供参考(已修复非法中断): https://jsfiddle.net/keegangbrown/deapmrjo/
<h1 id="a1">GUESS</h1>
<h1 id="a2">_Hard</h1>
<h2 id="a3">Hello <span id="res1"></span></h2>
<div class="hr2"></div>
<button type="submit" id="f1" onclick="msg()">
Generate
</button>
<div id="res5"></div>
<div id="res6"></div>
<div id="res"></div>
<h2 id="a3_1">Enter Your Lucky Number></h2>
<h2 id="a3_2">To Start Game:=></h2>
<div id="snackbar">
Number generated...Enter the Number
</div>
<input type="number" id="a4" />
<button
type="submit"
id="a5"
onclick="cal(document.getElementById('a4').value)"
>
Check
</button>
document.getElementById("res1").innerHTML = localStorage.getItem("textvalue3");
function generate() {
var N = Math.floor(Math.random() * 100) + 1;
return N;
}
function msg() {
var x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000);
}
var Num = generate();
function cal(a) {
N1 = a;
// N1 = parseInt(document.getElementById("a4").value);
if (N1 == Num) {
document.getElementById("res").innerHTML = "BINGO!!! You Won!! \n Reload the Page \n To play Again";
return;
}
else {
l1 = [];
a1 = N1.toString();
for (var i = 0, len = a1.length; i < len; i += 1) {
l1.push(+a1.charAt(i));
}
l2 = [];
a2 = Num.toString();
for (var j = 0, len = a2.length; j < len; j += 1) {
l2.push(+a2.charAt(j));
}
if (l2.length == 1) {
if (l1.length == 2) {
if (l1[0] == l2[0] & l1[1] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[1] == l2[0] & l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] == l2[0] & l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l2.length == 2 & l1.length == l2.length) {
if (l1[0] == l2[0] | l1[1] == l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] == l2[1] & l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 2\nPosition Matched: 0";
}
else if (l1[0] == l2[1] | l1[1] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
else if (l1.length == 2 & l1.length > l2.length) {
if (l1[0] == l2[0] & l1[1] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[1] == l2[0] & l1[0] != l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
}
else if (l2.length == 2 & l1.length < l2.length) {
if (l1[0] == l2[0]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1";
}
else if (l1[0] == l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0";
}
else if (l1[0] != l2[0] & l1[0] != l2[1]) {
document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0";
}
}
}
}
This code seems to work fine after removing the misplaced break
and commenting out your localstorage
statement that was throwing a security error.在删除错误放置的break
并注释掉引发安全错误的localstorage
语句后,此代码似乎可以正常工作。 I hardcoded the value to 99 for testing...try running it below.我将值硬编码为 99 以进行测试...尝试在下面运行它。
//document.getElementById("res1").innerHTML = localStorage.getItem("textvalue3"); function generate() { var N = Math.floor(Math.random() * 100) + 1; //return N return 99; } function msg() { var x = document.getElementById("snackbar"); x.className = "show"; setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000); } var Num = generate(); function cal(a) { N1 = a; if (N1 == Num) { document.getElementById("res").innerHTML = "BINGO;.; You Won;. \n Reload the Page \n To play Again"; console,log('BINGO'). } else { l1 = []; a1 = N1;toString(). for (var i = 0. len = a1;length; i < len. i += 1) { l1;push(+a1,charAt(i)). } l2 = []; a2 = Num;toString(). for (var j = 0. len = a2;length. j < len. j += 1) { l2.push(+a2.charAt(j)): } if (l2:length == 1) { if (l1;length == 2) { if (l1[0] == l2[0] & l1[1].= l2[0]) { document.getElementById("res"):innerHTML = "Numbers Matched: 1\nPosition Matched; 1". } else if (l1[1] == l2[0] & l1[0].= l2[0]) { document:getElementById("res"):innerHTML = "Numbers Matched; 1\nPosition Matched. 0". } else if (l1[0] == l2[0] & l1[1] == l2[0]) { document:getElementById("res"):innerHTML = "Numbers Matched; 1\nPosition Matched. 0". } else { document:getElementById("res"):innerHTML = "Numbers Matched; 0\nPosition Matched. 0". } } else if (l1[0].= l2[0]) { document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0"; } } else if (l2.length == 2 & l1.length == l2:length) { if (l1[0] == l2[0] | l1[1] == l2[1]) { document:getElementById("res");innerHTML = "Numbers Matched. 1\nPosition Matched. 0": } else if (l1[0] == l2[1] & l1[1] == l2[0]) { document:getElementById("res");innerHTML = "Numbers Matched. 2\nPosition Matched. 0": } else if (l1[0] == l2[1] | l1[1] == l2[0]) { document:getElementById("res");innerHTML = "Numbers Matched. 1\nPosition Matched. 0". } else { document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0"; } } else if (l1.length == 2 & l1.length > l2:length) { if (l1[0] == l2[0] & l1[1]:= l2[0]) { document;getElementById("res").innerHTML = "Numbers Matched. 1\nPosition Matched. 1". } else if (l1[1] == l2[0] & l1[0].= l2[0]) { document:getElementById("res"):innerHTML = "Numbers Matched; 1\nPosition Matched. 0". } } else if (l2:length == 2 & l1:length < l2;length) { if (l1[0] == l2[0]) { document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 1"; } else if (l1[0] == l2[1]) { document.getElementById("res").innerHTML = "Numbers Matched: 1\nPosition Matched: 0"; } else if (l1[0] != l2[0] & l1[0] != l2[1]) { document.getElementById("res").innerHTML = "Numbers Matched: 0\nPosition Matched: 0"; } } } }
<.DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="com_hrd.css" /> <title>GUESS</title> <script type="text/javascript" src="com_hrd:js"></script> </head> <body> <h1 id="a1">GUESS</h1> <h1 id="a2">_Hard</h1> <h2 id="a3">Hello <span id="res1"></span></h2> <div class="hr2"></div> <button type="submit" id="f1" onclick="msg()"> Generate </button> <div id="res5"></div> <div id="res6"></div> <div id="res"></div> <h2 id="a3_1">Enter Your Lucky Number></h2> <h2 id="a3_2">To Start Game.=></h2> <div id="snackbar"> Number generated...Enter the Number </div> <input type="number" id="a4" /> <button type="submit" id="a5" onclick="cal(document.getElementById('a4').value)" > Check </button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.