【问题标题】:What is the logic of html and js? [closed]html和js的逻辑是什么? [关闭]
【发布时间】:2021-12-29 03:13:24
【问题描述】:

我有一个小测试。当我点击“点击”按钮时,显示效果很好,颜色会改变,但是console.log显示代码没有按照我输入的逻辑运行。

一开始,颜色是黑色的,没错 我点击按钮颜色变成红色,这是正确的 但是看控制台,我认为必须先显示“显示栏”。

我可能做错了什么,或者我的逻辑错了。请帮帮我。

function hideShow() {
  const footer = document.getElementById("footer");
  authorInfo = document.getElementById("authorInfo");
  if (footer.style.display === "none") {
    footer.style.display = "block";
    authorInfo.style.display = "none";
    console.log("show bar");
  } else {
    footer.style.display = "none";
    authorInfo.style.display = "flex";
    console.log("hide bar");
  }
}
<div id="footer" class="item" style="
        width: 100px;
        height: 200px;
        background-color: black;
        display: flex;
      "></div>
<div id="authorInfo" class="object" style="width: 100px; height: 200px; background-color: red; display: none"></div>
<button type="button" onclick="hideShow()">click</button>

【问题讨论】:

  • if (footer.style.display === "none") { 不是。它是弹性的
  • 我知道了,问题解决了,非常感谢
  • 随意删除问题

标签: javascript html onclick display


【解决方案1】:

您正在检查 if (footer.style.display === "none"),根据您提供的 HTML,这是错误的,因为它包含 display: flex; 属性。因此,条件的else 块被执行。

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 2017-06-25
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多