如何使用javascript显示div

时间:2018-01-01 08:10:57

标签: javascript

function Myfunction1() {

var a = document.getElementById("signup");
    var b = document.getElementById("loginpage1");

    if (a.style.display === "none") {
        a.style.display = "block";
        b.style.display = "none";

    } else {

        b.style.display = "none";
    }
}

2 个答案:

答案 0 :(得分:0)

example会帮助你。

function Myfunction1() {

var a = document.getElementById("signup");
var b = document.getElementById("loginpage1");

if (a.style.display === "none") {
    a.style.display = "block";
    b.style.display = "none";

} else {
    a.style.display = "none";
    b.style.display = "block";
}

}

答案 1 :(得分:0)

当您通过CSS选择器特别从外部样式表文件隐藏时,a.style.display === "none"始终不起作用。要克服这种情况,请尝试以下方式:

function Myfunction1() {

var a = document.getElementById("signup");
var b = document.getElementById("loginpage1");

if (a.offsetParent === null) {
    a.style.display = "block";
    b.style.display = "none";

} else {
    a.style.display = "none";
    b.style.display = "block";
}
}