gpt4 book ai didi

javascript - 破坏 JavaScript 代码的特殊字符

转载 作者:行者123 更新时间:2023-12-03 03:40:17 26 4
gpt4 key购买 nike

我正在为机械网站制作 JavaScript 闪卡游戏。因为我想将方程写在卡片上,所以我需要使用 delta(Δ) 符号。

一张卡片可能有:一侧是“功率方程”,另一侧是“P=W/Δt”。如果卡片从第一面开始,当按下空格键或按下翻转按钮时,它将翻转。但是,如果它从第二侧(带有 Δ 符号的一侧)开始,则按下“翻转”按钮或空格键时它不会翻转。

我尝试过不同的写法:

Δ Δ Δ

这些都不起作用。我的代码是:

//Copyright Attribution-ShareAlike 4.0 International 2017 Hazel Meehan


//array containing all options
var options = [ //counts from 0
"Joules(J)", "Measure of Work",
"Watts(W)", "Measure of Power",
"Ek", "Kinetic Energy",
"Ep", "Potential Energy",
"Newtons(N)", "Measure of Force",
"Pascals(Pa)", "Pressure",
"ms-1", "Metres per Second",
"ms-2", "Metres per Second Gained",
"10N", "Value of Gravity",
"Weight is a", "Force beginning with W",
"The capacity to do work", "Energy is",
"d = ΔvΔt is the equation for", "The equation for distance",
"W=FΔd is the equation for", "The equation for work",
"P=W/Δt is the equation for", "The equation for power"
];

//initialize variables
var randomNum = 0;
var sideOne = " ";
var sideTwo = " ";

//choose new card using random number
var newCard = function() { //runs on 'next'
var randomNum = Math.floor(Math.random() * options.length);
sideOne = options[randomNum];
if (randomNum % 2 == 0) { //is the number even
sideTwo = options[randomNum + 1];
} else {
sideTwo = options[randomNum - 1];
}
document.getElementById("card").innerHTML = sideOne;
};

//show other side of card
var flip = function() { //runs on 'flip'
if (document.getElementById("card").innerHTML == sideOne) {
document.getElementById("card").innerHTML = sideTwo;
} else {
document.getElementById("card").innerHTML = sideOne;
}
};

//change card on key down
document.onkeydown = function(e) {
e = e || window.event;
if (e.keyCode == '39') { //right arow key
newCard();
} else if (e.keyCode == '32') { //space bar
flip();
}
}
<article>
<h2>Flashcards</h2>
<center><button id="flashButton" onclick="newCard()">Next</button></center>
<br>
<center><button id="card"></button></center>
<br>
<center><button id="flashButton" onclick="flip()">Flip</button></center>
</article>

最佳答案

检查元素的innerHtml(if (document.getElementById("card").innerHTML == sideOne))时,之前设置的字符串已被解释为html当检索它时,Δ 现在将是 Δ。那么比较结果是错误的:

"d = ΔvΔt 是"!== "d = ΔvΔt 是"的方程式

因此它再次将 innerHtml 设置到同一侧。

关于javascript - 破坏 JavaScript 代码的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45655827/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com