我在点击网站上的按钮时使用此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>
我正在使用<A Onclick="return toggleMe('content')" >[Website Content]</A>
问题是我有三个&#34;按钮&#34; (帮助,gpgkey,内容),我希望当时只显示一个文本。我之前从未做过java,我甚至不确定完全理解第一个代码。
这是我的一次尝试
<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;
}
答案 0 :(得分: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";
}
}