【问题标题】:JS - Set visibility of a div to hidden via a button [duplicate]JS - 通过按钮将 div 的可见性设置为隐藏
【发布时间】:2020-08-29 04:28:53
【问题描述】:

function button1() {
    document.getElementById("id1").style.visibility = "hidden";
}
.d1 {
    font-family: "Aldrich";
    font-size: 50px;
}
<link href="https://fonts.googleapis.com/css?family=Aldrich" rel="stylesheet">

<div class="d1" id ="id1">Test</div>

<button onclick="button1">Button 1</button>

我使用上面的简单代码通过buttondivvisibility 设置为hidden。我点击它但没有任何反应。如果我将属性 visibility: hidden; 直接设置为 d1 它确实可以工作...

【问题讨论】:

  • 你只是忘记了括号:onclick="button1" 变成 onclick="button1()"

标签: javascript css visibility


【解决方案1】:

给你一个解决方案

function button1() {
  document.getElementById("id1").style.visibility = "hidden";
}
.d1 {
    font-family: "Aldrich";
    font-size: 50px;
}
<div class="d1" id ="id1">Test</div>

<button onclick="button1()">Button 1</button>

您忘记从按钮单击调用该方法。 用于调用方法button1()

【讨论】:

    【解决方案2】:

    在您的 html 中缺少 button1() &lt;button&gt;

    function button1() {
        document.getElementById("id1").style.visibility = "hidden";
    }
    .d1 {
        font-family: "Aldrich";
        font-size: 50px;
    }
    <div class="d1" id ="id1">Test</div>
    
    <button onclick="button1()">Button 1</button>

    【讨论】:

      猜你喜欢
      • 2012-05-10
      • 1970-01-01
      • 2013-02-02
      • 2017-07-02
      • 2022-08-13
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多