我已经完成了一个项目并且运行得很完美。我不断收到错误通知,其中包含'记录'没有定义。每次我处理文档时都会收到相同的通知。有没有办法可以防止它被识别为错误?
JS:
function check_prime() {
var input = document.getElementById("enteredNumber").value;
var number = parseInt(input);
if (isNaN(number)) {
alert("Oops! Please enter a valid number.");
document.getElementById("enteredNumber").value="";
document.getElementById("answer").innerHTML = "";
document.getElementById("enteredNumber").focus();
}
else if (input.length === 0) {
alert("Please enter a valid input");
document.getElementById("enteredNumber").focus();
}
else if (!isNaN(number)) {
var divisors = is_perfect(number);
if (divisors) {
document.getElementById("answer").innerHTML = "Congratulations! " + number + " is a perfect number.";
alert("Congratulations! " + number + " is a perfect number. The divisors are: " + divisors.toString() + ".") ;
}
else {
document.getElementById("answer").innerHTML = "I'm sorry! " + number + " is not a perfect number."
}
}
else {
document.getElementById("answer").innerHTML = "Please enter a number.";
}
}
function is_perfect(number) {
var temp = 0;
var divisors = [];
for(var i=1;i<=number/2;i++) {
if (number%i === 0) {
divisors.push(i);
temp += i;
}
}
return temp === number ? divisors : null;
}
function clear_textbox(){
document.getElementById("answer").innerHTML = "";
document.getElementById("enteredNumber").value="";
document.getElementById("enteredNumber").focus();
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Oswald:700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" rel="stylesheet">
</head>
<body>
<h2> PERFECTION </h2>
<h3> PLEASE ENTER A NUMBER: </h3><br>
<div>
<input type="text" id="enteredNumber" autofocus value = "" /> <br><br>
<input type="submit" value="Check"
title="Perfection"
onclick="check_prime()" />
<input type="submit" value="Clear"
title="Click here to clear the text box."
onclick="clear_textbox()" />
<br><br>
<p id="answer"></p>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
<script src="app.js">
</script>
我也收到错误警告&#39;没有定义......我不知道我是否遗漏了什么。
答案 0 :(得分:2)
尝试添加
/*eslint-env browser*/
在顶部。
错误:未定义'文档'。 [no-undef] document.getElementById(“answer”)。innerHTML =“”;
此错误是一个lint错误(请参阅eslint [no-undef])。它不会影响您在浏览器中运行的代码,但是当检测到lint错误时,您的本地构建可能会停止运行。
您可能正在使用eslint
,因此请查看this guide,然后配置
{
"env": {
"browser": true
}
}
在.eslintrc
文件中。
或许更简单的方法就是添加此评论:
/*eslint-env browser*/
您的代码似乎正在运行。您是否在浏览器控制台中收到该错误?如果不是,它可能只是一个lint错误。
粘贴您收到的确切错误消息真的很有帮助。
function check_prime() {
var input = document.getElementById('enteredNumber').value;
var number = parseInt(input);
if (isNaN(number)) {
alert('Oops! Please enter a valid number.');
document.getElementById('enteredNumber').value = '';
document.getElementById('answer').innerHTML = '';
document.getElementById('enteredNumber').focus();
} else if (input.length === 0) {
alert('Please enter a valid input');
document.getElementById('enteredNumber').focus();
} else if (!isNaN(number)) {
var divisors = is_perfect(number);
if (divisors) {
document.getElementById('answer').innerHTML =
'Congratulations! ' + number + ' is a perfect number.';
alert(
'Congratulations! ' +
number +
' is a perfect number. The divisors are: ' +
divisors.toString() +
'.'
);
} else {
document.getElementById('answer').innerHTML =
"I'm sorry! " + number + ' is not a perfect number.';
}
} else {
document.getElementById('answer').innerHTML = 'Please enter a number.';
}
}
function is_perfect(number) {
var temp = 0;
var divisors = [];
for (var i = 1; i <= number / 2; i++) {
if (number % i === 0) {
divisors.push(i);
temp += i;
}
}
return temp === number ? divisors : null;
}
function clear_textbox() {
document.getElementById('answer').innerHTML = '';
document.getElementById('enteredNumber').value = '';
document.getElementById('enteredNumber').focus();
}
<h2> PERFECTION </h2>
<h3> PLEASE ENTER A NUMBER: </h3><br>
<div>
<input type="text" id="enteredNumber" autofocus value="" /> <br><br>
<input type="submit" value="Check" title="Perfection" onclick="check_prime()" />
<input type="submit" value="Clear" title="Click here to clear the text box." onclick="clear_textbox()" />
<br><br>
<p id="answer"></p>
</div>
答案 1 :(得分:0)
要消除此错误,您可以使用eslint。对于Brackets编辑器,请安装brackets-eslint