单击按钮时如何隐藏<a>标记?

yxyvkwin  于 2022-10-22  发布在  Java
关注(0)|答案(5)|浏览(178)

我想这样做,当有人点击转义键时,它会隐藏标签。我该怎么做?这是我当前的代码:

boxid = "div";
hidden = "false";
window.onkeyup = function(event) {
  if (event.keyCode == 27) && hidden = "true" {
    document.getElementById(boxid).style.visibility = "block";
    hidden = "false"
  }
}
window.onkeyup = function(event) {
  if (event.keyCode == 27) && hidden = "true" {
    document.getElementById(boxid).style.visibility = "hidden";
    hidden = "true"
  }
}
<center id="div">
  <div style="width: 100%;position: fixed;background: white;display: flex;justify-content: center;align-items: center;text-align: center;overflow: hidden;">
    <a href="home">
      <img src="https://www.freeiconspng.com/thumbs/homepage-icon-png/house-icon-png-white-32.png" width="35px" height="35px">
    </a>
</center>

谢谢大家的帮助!我得到了很多答案,我没有注意到我做错了什么,我会检查答案,看看有什么效果!对不起,如果我不清楚,我只是想把标签藏起来。

gcmastyq

gcmastyq1#

代码中的一些错误
1-您忘记关闭<div>标记
2-条件是错误的if (event.keyCode == 27) && hidden = "true" {&& hidden="true"在括号外,而且hidden="true"表示您正在给hidden一个新值,而不是询问hidden的值是否为true,因此您必须使用==进行比较
3-不需要2 onkeyup函数,只需使用and else语句

var boxid = "div";
var hidden = "false";
window.onkeyup = function(event) {
  if (event.keyCode == 27 && hidden == "true") {
    console.log('display');
    document.getElementById(boxid).style.display = "inline";
    hidden = "false"
  } else {
    console.log('hide');
    document.getElementById(boxid).style.display = "none";
    hidden = "true"
  }
}
<center id="div">
  <div style="width: 100%;position: fixed;background: white;display: flex;justify-content: center;align-items: center;text-align: center;overflow: hidden;">
    <a href="home">
      <img src="https://www.freeiconspng.com/thumbs/homepage-icon-png/house-icon-png-white-32.png" width="35px" height="35px">
    </a>
  </div>
</center>

您还可以使用document.getElementById(boxid).style.visibility = "initial";document.getElementById(boxid).style.visibility = "hidden";

utugiqy6

utugiqy62#

你的代码有2个错误
1.如果条件没有正确的括号。
1.您正在使用赋值运算符=,而不是相等检查运算符m1n 1o1p进行比较。
这是正确的代码-

boxid = "div";
hidden = "true";
window.onkeyup = function(event) {
  if ((event.keyCode == 27) && hidden == "true") {
    document.getElementById(boxid).style.visibility = "block";
    hidden = "false"
  }
}
window.onkeyup = function(event) {
  if ((event.keyCode == 27) && hidden == "true") {
    document.getElementById(boxid).style.visibility = "hidden";
    hidden = "true"
  }
}

此外,为了改进代码,对于hidden变量,可以使用boolean类型作为标志值。
下面是您可以尝试的改进代码-

boxid = "div";
hidden = true;;
window.onkeyup = function(event) {
  if ((event.keyCode == 27) && hidden ) {
    document.getElementById(boxid).style.visibility = "block";
    hidden = false
  }
}
window.onkeyup = function(event) {
  if ((event.keyCode == 27) && hidden) {
    document.getElementById(boxid).style.visibility = "hidden";
    hidden = true
  }
}
c9qzyr3d

c9qzyr3d3#

我不太明白你想做什么,但你的代码有一些错误,如果你想做一个可见性切换,我想这就是你需要的代码

<script>
  var boxid = "div";
  var hidden = false;

  window.addEventListener("keyup", (e) => {
    if (event.keyCode === 27) {
      document.getElementById(boxid).style.visibility = hidden ? "hidden" : "initial";
      hidden = !hidden;
    }
  })
</script>
hgtggwj0

hgtggwj04#

最好的做法是(IMO)只有一个“keyup”事件函数,而不是两个,如下所示:

boxid = "div";
hidden = true;;
window.onkeyup = function(event) {
    if (event.keyCode == 27) {    
        if (hidden) { //short version for hidden == true
            document.getElementById(boxid).style.visibility = "block";
            hidden = false;
        } else {
            document.getElementById(boxid).style.visibility = "hidden";
            hidden = true;
        }
    }
}
csga3l58

csga3l585#

下面是它的另一个改进,它改进了预览答案并生成了代码ES6(javascript的最后一个版本)。以及对我添加的代码的一些评论。

<script>
  // Get the div element and store a reference to it in box variable
  const box = document.getElementById("div");

  window.addEventListener("keyup", (e) => {
    // if 'Esc' is pressed AND the visibility is NOT "hidden"
    if (event.keyCode === 27 && box.style.visibility !== "hidden") {
       // set visibility to "hidden"
       box.style.visibility = "hidden";
    }
  })
</script>

不确定原因,但也要求取消隐藏

<script>
  // Get the div element and store a reference to it in box variable
  const box = document.getElementById("div");
  // store the original visibility setting, better in case you change it in the future using html.
  let boxStateOrg = box.style.visibility;

  window.addEventListener("keyup", (e) => {
    // if 'Esc' is pressed AND the visibility is NOT "hidden"
    if (event.keyCode === 27) {
      if (box.style.visibility !== "hidden") {
        // set visibility to "hidden"
        box.style.visibility = "hidden";
      } else {
        box.style.visibility = boxStateOrg;
      }
    }
  })
</script>

相关问题