无法使用JS设置可见性

时间:2018-07-01 14:35:40

标签: javascript css button

我试图通过单击按钮来切换列的可见性,但是该按钮似乎不起作用。有人可以帮我吗?

我要切换的列:      {{Kanji}}

function toggleKH() {
  var i;
  var kcol = document.getElementsByClassName("kanjiColumn");
  var hcol = document.getElementsByClassName("hiraganaColumn");
  for (i = 0; i < kcol.length; i++) {
    if (kcol[i].style.visibility === "visible") {
      kcol[i].style.visibility === "hidden";
    } else {
      kcol[i].style.visibility === "visible";
    }
  }
}
.kanjiColumn,
.hiraganaColumn {
  visibility: visible;
}
<button onclick="toggleKH()">x</button>


<td class="kanjiColumn">{{Kanji}}</td>

1 个答案:

答案 0 :(得分:1)

在if-else语句内用=(赋值运算符)替换===(等式条件运算符):

   if (kcol[i].style.visibility === "visible"){
            kcol[i].style.visibility = "hidden";
        } else {
            kcol[i].style.visibility ="visible";
        }