【发布时间】:2021-03-19 05:05:02
【问题描述】:
我有一个名为“check”的按钮,它最初使用 Bootstrap 的“btn btn-primary btn-lg”类名。如果显示的函数条件为真,我希望按钮的类更改为“btn btn-success”,如果不满足条件,请将类更改为“btn btn-danger”。我在页面上有多个按钮,它们具有名为“btn btn-primary btn-lg”的相同类,因此我无法按类类型选择它,我假设它必须使用按钮 ID。我所拥有的不起作用,我收到错误消息“未捕获的 DOMException:字符串包含无效字符 game_logic.js:34”。有谁知道这个错误指的是什么?
我还希望“检查”按钮在单击“检查”后根据条件显示“正确”或“不正确”。
const newProblemBtn = document.querySelector('#start');
const checkBox = document.querySelector('#splash_screen_preference_check_box');
const randomFunc = [
multiplication,
division,
addition,
subtraction,
]
let mathProblem = document.querySelector('#math_problem').innerText
newProblemBtn.addEventListener('click', () => {
let result = randomFunc[Math.floor(Math.random() * randomFunc.length)]();
document.querySelector('#correct_answer').setAttribute('value', result);
});
document.querySelector('#result_check').addEventListener('click', () => {
if (document.querySelector('#correct_answer').getAttribute('value') === document.querySelector('#user_input').value) {
document.querySelector('#result_check').classList.remove('btn btn-primary btn-lg');
document.querySelector('#result_check').classList.add('btn btn-success');
} else {
document.querySelector('#result_check').classList.remove('btn btn-primary btn-lg');
document.querySelector('#result_check').classList.add('btn btn-danger');
}
});
function multiplication() {
let num1 = Math.floor(Math.random() * 13);
let num2 = Math.floor(Math.random() * 13);
let problemResult = num1 * num2;
console.log(num1, '*', num2, '=', problemResult);
document.getElementById('math_problem').innerHTML =
(`${num1} x ${num2}`);
return problemResult
}
function division() {
let num1 = Math.floor(Math.random() * 13);
let num2 = Math.floor(Math.random() * 12) + 1;
let problemResult = (num1 * num2) / num2;
console.log(num1 * num2, '/', num2, '=', problemResult);
document.getElementById('math_problem').innerHTML =
(`${num1 * num2} ÷ ${num2}`);
return problemResult
}
function addition() {
let num1 = Math.floor(Math.random() * 13);
let num2 = Math.floor(Math.random() * 13);
let problemResult = num1 + num2;
console.log(num1,'+',num2,'=',problemResult);
document.getElementById('math_problem').innerHTML =
(`${num1} + ${num2}`);
return problemResult
}
function subtraction() {
let num1 = Math.floor(Math.random() * 13);
let num2 = Math.floor(Math.random() * 13);
let numList = [num1, num2];
numList.sort(function (a, b) {
return a - b
});
let problemResult = numList[1] - numList[0];
console.log(numList[1], '-', numList[0], '=', problemResult);
document.getElementById('math_problem').innerHTML =
(`${numList[1]} - ${numList[0]}`);
return problemResult
}
<div class="col text-center">
<button id="start" type="button" class="btn btn-primary btn-lg">
New Problem
</button>
<p id="math_problem"></p>
<form action="">
<input id="user_input" type="text" placeholder="Type your answer here">
<input id="correct_answer" type="hidden">
</form>
<br>
<button id="result_check" type="button" class="btn btn-primary btn-lg">Check</button>
<script src={% static 'js/game_logic.js' %}></script>
</div>
【问题讨论】:
-
错误本身看起来是指第 34 行的语法错误,特别是无效字符。第 34 行实际上可能包含也可能不包含该无效字符,请检查它之前的一行或多行,看看那里是否存在可能导致该字符稍后无效的语法错误。至于你的按钮,如果你想在多个类上重用相同的逻辑,你可以在按钮类上做一个事件监听器,并在事件监听器中运行一些逻辑。这可能会帮助stackoverflow.com/a/19655662/1174822
标签: javascript html button bootstrap-4