javascript - 为什么 JavaScript RegExp 在 Firefox 中不起作用?
问题描述
有人能告诉我为什么我的代码不能在 Firefox 中运行吗?在其他浏览器中没问题,但如果我在 Cash at Bank 单元格中输入 1000,它会将其标记为不正确的答案(以红色突出显示并显示不正确的反馈)。其他单元格条目的其他验证/测试也不起作用。
function checkAnswers() {
document.querySelectorAll('[data-answer]').forEach(item => {
if (RegExp(item.dataset.answer).test(item.innerText)) {
item.style.backgroundColor = "#e2edde";
item.title = item.dataset.correct;
} else {
item.style.backgroundColor = "#fce9e9";
item.title = item.dataset.incorrect;
}
})
document.getElementById("feedbackdiv").style.display = "inline-block";
document.getElementById("feedback").innerText = "Click in each highlighted cell for more information.";
document.getElementById("feedbackdiv").style.backgroundColor = "rgb(234, 243, 250)";
document.getElementById("checkbutton").style.display = "none";
document.getElementById("retrybutton").style.display = "block";
}
function showFeedback() {
var cell = document.activeElement;
if (cell.style.backgroundColor == "rgb(226, 237, 222)") {
document.getElementById("feedback").innerText = cell.dataset.correct;
document.getElementById("feedbackdiv").style.backgroundColor = "rgb(226, 237, 222)";
}
if (cell.style.backgroundColor == "rgb(252, 233, 233)") {
document.getElementById("feedback").innerText = cell.dataset.incorrect;
document.getElementById("feedbackdiv").style.backgroundColor = "rgb(252, 233, 233)";
}
}
function showSolution() {
document.querySelectorAll('[data-answer]').forEach(item => {
item.style.backgroundColor = "#eaf3fa";
item.innerText = item.dataset.solution;
})
document.getElementById("feedbackdiv").style.display = "inline-block";
document.getElementById("feedback").innerText = "In the answer you can see that Cash at bank has increased by CU1,000 and equity has increased by CU1,000."; document.getElementById("feedbackdiv").style.backgroundColor = "rgb(234, 243, 250)"; document.getElementById("checkbutton").style.display = "none";
document.getElementById("solution").style.display = "none"; document.getElementById("retrybutton").style.display = "block";
}
<table border="1">
<tbody>
<tr>
<th scope="col"> </th>
<th scope="col">Cash at <br> Bank </th>
<th scope="col">Vehicle</th>
<th scope="col">Equipment</th>
<th scope="col">Supplies</th>
<th scope="col">Accounts <br> Receivable </th>
<th scope="col"> </th>
<th scope="col">Accounts <br> Payable </th>
<th scope="col"> </th>
<th scope="col">Otok, Equity </th>
</tr>
<tr>
<td>(6)</td>
<td id="cashanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^(1000|1\,000)$" data-solution="1,000" data-incorrect="Incorrect. We neeed to show that Cash at Bank has increased by CU1,000." data-correct="Well done. Cash at Bank has increased by CU1,000."
onfocus="showFeedback()"></td>
<td id="vehicleanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^$" data-incorrect="Incorrect. This cell should be left blank." data-correct="Well done. This cell should be left blank." onfocus="showFeedback()"></td>
<td id="equipmentanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^$" data-incorrect="Incorrect. This cell should be left blank." data-correct="Well done. This cell should be left blank." onfocus="showFeedback()"></td>
<td id="suppliesanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^$" data-incorrect="Incorrect. This cell should be left blank." data-correct="Well done. This cell should be left blank." onfocus="showFeedback()"></td>
<td id="receivableanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^$" data-incorrect="Incorrect. This cell should be left blank." data-correct="Well done. This cell should be left blank." onfocus="showFeedback()"></td>
<td></td>
<td id="payableanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^$" data-incorrect="Incorrect. This cell should be left blank." data-correct="Well done. This cell should be left blank." onfocus="showFeedback()"></td>
<td></td>
<td id="equityanswer" title="Click in a cell to type your entry" contenteditable="true" data-answer="^(1000|1\,000)$" data-solution="1,000" data-incorrect="Incorrect. We neeed to show that Equity has increased by CU1,000." data-correct="Well done. Equity has increased by CU1,000."
onfocus="showFeedback()"></td>
</tr>
</tbody>
</table>
<div id="feedbackdiv" style="clear: both; padding-left: 15px; padding-right: 15px; display: none; margin-top: 20px;">
<p id="feedback" style="margin-top: 15px;"> </p>
</div>
<p style="margin-top: 20px;">
<button class="btn btn-secondary" id="checkbutton" onclick="checkAnswers()">Check answers</button>
</p>
<p id="retrybutton" style="display: none">
<button onclick="window.location.reload();" class="btn btn-secondary">Retry</button>
</p>
<p id="solution" style="margin-top: 20px;">
<button class="btn btn-secondary" id="solutionbutton" onclick="showSolution()">Show solution</button>
</p>
解决方案
Firefox 回馈“1000\n”,Chrome 回馈“1000”,因此将多行的“m”添加到您的正则表达式帐户中
document.querySelectorAll('[data-answer]').forEach(item => {
if (RegExp(item.dataset.answer, 'm').test(item.innerText)) {
item.style.backgroundColor = "#e2edde";
item.title = item.dataset.correct;
} else {
item.style.backgroundColor = "#fce9e9";
item.title = item.dataset.incorrect;
}
})
推荐阅读
- go - 需要一个例子来说明赋值运算符的 Go 语法如何使用使用 EBNF 指定的语法规则
- java - 无法运行颤振医生--android-licenses
- intellij-idea - 我可以在不运行程序的情况下在 IntellIJ 中显示环境变量吗?
- c - 在 C 中加载不正确
- spring - 带有 Eclipselink 保存操作的 Spring JPA 不会持续存在
- metal - sample() 是否总是在 Metal Shading Language 中以 RGBA 格式返回 float4?
- flutter - 颤振 setState 未定义
- flutter - 有谁知道如何在没有页眉或页脚的情况下加载 webview 颤振?
- c - 为什么 Apple Clang 会在 -O1/2/3 下留下多余的堆栈推送弹出指令?
- java - Gradle 无法正确识别 mainModule