javascript - 折叠 window 并折叠所有其他 window 的代码

标签 javascript html function

函数折叠工作完美,但我希望第二个函数首先运行以首先关闭所有其他元素,但我不知道出了什么问题;谢谢您的帮助!

编辑。第二个功能似乎不起作用或关闭已经打开的表单。

编辑。谢谢大家的回答;明天我将有更多时间阅读并尝试一切。

函数.js:

function foldout(fold) {

    if (document.getElementById) {
        var fold = document.getElementById(fold).style;
            if (fold.display == "block") {
                fold.display = "none";
            } else {
                fold.display= "block";
            }
    return false;
    } else {
    return true;
    }
};
function foldall () {
    var foldall = document.getElementsByClassName('foldall');
        for(var i=0; i<foldall.length; i++) {
            var foldthis = foldall[i];
        if (foldthis.display == "block") { 
            foldthis.display = "none";
            }
        }
};

页面.php:

<a href="#" onclick="foldall('foldall'); return foldout('fold');"> </a>
<form method="POST" style="display: none;" class ="foldall" id="fold">
</form

最佳答案

尝试从 foldout 函数调用 foldall 函数:

function foldall () {
    var foldall = document.getElementsByClassName('foldall');
        for(var i=0; i<foldall.length; i++) {
            var foldthis = foldall[i];
        if (foldthis.display == "block") { 
            foldthis.display = "none";
            }
        }
};

function foldout(fold) {
    foldall(); // collapse everything
    if (document.getElementById) {
        var fold = document.getElementById(fold).style;
        if (fold.display == "block") {
            fold.display = "none";
        } else {
            fold.display= "block";
        }
        return false;
    } else {
        return true;
    }
};

并且仅从 onclick 处理程序中调用折叠:

<a href="#" onclick="foldout('fold');"></a>
<form method="POST" style="display: none;" class ="foldall" id="fold"></form>

关于javascript - 折叠 window 并折叠所有其他 window 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20273283/

相关文章:

c# - 第二次提交表单时的奇怪行为

function - 为什么在构建小部件之前,我的initState()不调用使用sharedprefs的函数?

c# - 在 API 中对参数验证抛出异常是否被认为是一种好的做法?

javascript - 我如何将鼠标悬停、鼠标移开和单击结合起来。没有 .mouseout 会毁掉 .click

javascript - 拆分一些粗体文本和一些不在功能性 javascript 片段上的文本

javascript - 为什么IE浏览器显示 "scanstyles does nothing in webkit/firefox internet explorer"?

css - <pre> 与滚动文本保持不同的行

javascript - 如何在渲染前向 props- 中传递的 HTML 标记添加属性?

c# - 如何模拟函数空间

javascript - 嵌套 React 组件和路由