代码问题"更改元素类"

时间:2018-03-11 13:08:05

标签: javascript html css

我的代码出现了问题,因为我想按照按钮按下来改变我的元素



var segSelectedy = "a";
var segSelectedx = 1;

function keydown(e) {
  if (e.keyCode == 37) {
    //left
    if (document.getElementById(segSelectedy + "" + (segSelectedx - 1)) != null) {
      document.getElementById("Element" + segSelectedy + "" + segSelectedx).className == "unselected";
      segSelectedx = segSelectedx - 1;
      document.getElementById("Element" + segSelectedy + "" + segSelectedx).className == "selected";
    }
  } else if (e.keyCode == 39) {
    //right
    if (document.getElementById(segSelectedy + "" + (segSelectedx + 1)) != null) {
      document.getElementById("Element" + segSelectedy + "" + segSelectedx).className == "unselected";
      segSelectedx = segSelectedx + 1;
      document.getElementById("Element" + segSelectedy + "" + segSelectedx).className == "selected";
    }
  }
}

document.onkeydown = keydown;

<body>
  <table class="console">
    <tr>
      <th id="a1"><button id="Elementa1" class="selected">Steam Games</button></th>
      <th id="a2"><button id="Elementa2" class="unselected">Settings</button></th>
    </tr>
  </table>
</body>
&#13;
&#13;
&#13;

申请:https://coffee-console.firebaseapp.com/

1 个答案:

答案 0 :(得分:2)

//                                                                              *
//                                                                              *
document.getElementById("Element" + segSelectedy + "" + segSelectedx).className = "unselected";

只需一个 =,否则不是作业。

这适用于上述各行。