破解JavaScript代码的特殊字符

时间:2017-08-12 23:35:49

标签: javascript special-characters

我正在为机械网站制作一个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>

1 个答案:

答案 0 :(得分:1)

检查元素innerHtmlif (document.getElementById("card").innerHTML == sideOne))时,先前设置的字符串已被解释为html,在检索时,&Delta;现在为{{1} }}。然后比较是错误的:

Δ

因此,它会再次将"d = &Delta;v&Delta;t is the equation for" !== "d = ΔvΔt is the equation for"设置为同一侧。