【问题标题】:Expand/Collapse div in javascript doesent work [closed]javascript中的展开/折叠div不起作用[关闭]
【发布时间】:2014-11-03 05:31:12
【问题描述】:

当我单击“主”div 时,尝试制作一个折叠/展开另一个 div 的脚本。 如果重要的话,两个 div 都是 100x100。

一个 100px x 100px div 包含 onclick="myFunction()",另一个是纯 html/css (id=box2)。

function expand() {
    document.getElementById("box2").style.height = "200px";
}

function collapse() { 
    document.getElementById("box2").style.height = "100px"; 
}

function myFunction() {
    if (document.getElementById('otherbox').clientHeight = 100) { 
     expand();
    }

    else { collapse(); }

}

尝试了这么多不同的替代方案,但仍然没有任何效果,建议? *编辑:div 展开了,但当我再次单击时它不会再次折叠。

【问题讨论】:

    标签: javascript html collapse expand


    【解决方案1】:

    .clientHeight = 100 替换为.clientHeight == 100

    使用 = 100 将值设置为 100,因此在您的 if 中始终返回 true。

    【讨论】:

      猜你喜欢
      • 2012-06-20
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2015-07-27
      • 2017-02-12
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      相关资源
      最近更新 更多