javascript,在按下按钮(多个按钮)时显示文本,但当时只显示一个文本

时间:2023-01-27 21:34:11

I'm using this javascript to display text when clicking a button in a website:

我在点击网站上的按钮时使用此javascript来显示文字:

<script type="text/javascript">
        function toggleMe(a)
        {
        var e=document.getElementById(a);
        if(!e)return true;
        if(e.style.display=="none")
            {e.style.display="block"}
        else
            {e.style.display="none"}
        return true;
        }
    </script>

and i'm calling the script with <A Onclick="return toggleMe('content')" >[Website Content]</A>

我用 [网站内容] 调用脚本

The problem is that i have three "buttons" (help, gpgkey, content) and i would like that only one text be displayed at the time. I've never done java before and Im not even sure of totally understanding the first code.

问题是我有三个“按钮”(帮助,gpgkey,内容),我希望当时只显示一个文本。我以前从未做过java,我甚至不确定完全理解第一个代码。

This is one of my attempts

这是我的一次尝试

<script type="text/javascript">
    function toggleMe(a)
    {
    var e=document.getElementById(a);
    if(!e)return true;

    if(e==document.getElementById("content")
        {
        var f=document.getElementById("help");
        var g=document.getElementById("gpgkey");
        }

    if(e==document.getElementById("help")
        {
        var f=document.getElementById("content");
        var g=document.getElementById("gpgkey");
        }

    if(e==document.getElementById("gpgkey")
        {
        var f=document.getElementById("content");
        var g=document.getElementById("help");
        }

    if(e.style.display=="none")
        {
        e.style.display="block"

        if(f.style.display!="none")
            {f.style.display="none"}

        if(g.style.display!="none")
            {g.style.display="none"}            
        }
    else
        {e.style.display="none"}

    return true;
    }

1 个解决方案

#1


1  

function toggleMe(a) {
    var eContent = document.getElementById("content"),
        eHelp    = document.getElementById("help"),
        eGPGKey  = document.getElementById("gpgkey");
    if(!eContent || !eHelp || !eGPGKey)
        return;

    if (a === "content" && eContent.style.display === "none") {
        eContent.style.display = "block";
        eHelp.style.display    = "none";
        eGPGKey.style.display  = "none";
    }
    else if (a === "help" && eHelp.style.display === "none") {
        eContent.style.display = "none";
        eHelp.style.display    = "block";
        eGPGKey.style.display  = "none";
    }
    else if (a === "gpgkey" && eGPGKey.style.display === "none") {
        eContent.style.display = "none";
        eHelp.style.display    = "none";
        eGPGKey.style.display  = "block";
    }
    else if (a === "content") {
        eContent.style.display = "none";
    }
    else if (a === "help") {
        eHelp.style.display = "none";
    }
    else if (a === "gpgkey") {
        eGPGKey.style.display = "none";
    }
}

http://jsfiddle.net/Z68p7/

#1


1  

function toggleMe(a) {
    var eContent = document.getElementById("content"),
        eHelp    = document.getElementById("help"),
        eGPGKey  = document.getElementById("gpgkey");
    if(!eContent || !eHelp || !eGPGKey)
        return;

    if (a === "content" && eContent.style.display === "none") {
        eContent.style.display = "block";
        eHelp.style.display    = "none";
        eGPGKey.style.display  = "none";
    }
    else if (a === "help" && eHelp.style.display === "none") {
        eContent.style.display = "none";
        eHelp.style.display    = "block";
        eGPGKey.style.display  = "none";
    }
    else if (a === "gpgkey" && eGPGKey.style.display === "none") {
        eContent.style.display = "none";
        eHelp.style.display    = "none";
        eGPGKey.style.display  = "block";
    }
    else if (a === "content") {
        eContent.style.display = "none";
    }
    else if (a === "help") {
        eHelp.style.display = "none";
    }
    else if (a === "gpgkey") {
        eGPGKey.style.display = "none";
    }
}

http://jsfiddle.net/Z68p7/