将函数参数传递给getElementById“ id”

era*_*s77 4 javascript arguments function getelementbyid

这是一个js函数,当您从选择框中选择合适的值时,它会显示各种文本输入形式。

function arata_formular(formular) {
            document.getElementById("formular").style.visibility = "visible";
            if(document.getElementById("formular").style.display == "none" ) {
                document.getElementById("formular").style.display = "inline";
            }
            else {
                document.getElementById("formular").style.display = "visible";
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是不能按预期工作。尽管它有一个参数,无论我要传递给它什么(让我们说arata_formular(entropy),它仍然会寻找“形式”的id而不是“熵”。我怎样才能使“ inline”插入?

不幸的是,我不能在此框架或其他框架上使用jquery。我必须只使用javascript。谢谢!

Asc*_*rer 5

只是摆脱报价。

function arata_formular(formular) {
    var el = document.getElementById( formular );

    el.style.visibility = "visible";
    el.style.display = el.style.display === "none" ? "inline" : "visible";
}
Run Code Online (Sandbox Code Playgroud)

要么


function arata_formular(formular) {
    document.getElementById( formular ).style = {
        visibility: "visible",
        display: el.style.display === "none" ? "inline" : "visible"
    }
}
Run Code Online (Sandbox Code Playgroud)