我在我的主页上有3个特定部分。 我想要做的是设置链接,调用一个javascript函数,确保2个部分被隐藏,并且只显示按钮被点击的部分。使用Javascript切换内容
这里是我的代码:
<div id="idc" class="leftFloat"><span id="title" class="title1">Introduction</span></div>
<div class="rightFloat">
<div id="agri"><a onclick="ContentSwitch('Agri');">Agri Industries</a></div>
<div id="ict"><a onclick="ContentSwitch('ict');">ICT Investments</a></div>
<div id="intro"><a onclick="ContentSwitch('intro');">Introduction</a></div>
</div>
<div id="agriContent" style="display: none;">
<div class="vrtlay_both">AGRI INDUSTRIES</div>
</div>
<div id="ictContent" style="display: none;">
<div class="vrtlay_both">ICT INVESTMENTS</div>
</div>
<div id="introContent">
<div class="vrtlay_both">ICT INVESTMENTS</div>
</div>
<script type="text/javascript">
function ContentSwitch(id) {
if (id = "Agri") {
if (document.getElementById("agriContent").style.display = "none") {
document.getElementById("agriContent").style.display = "block";
// Hide other content
document.getElementById("ictContent").style.display = "none";
document.getElementById("introContent").style.display = "none";
// Change the look of the title
document.getElementById("idc").style.backgroundColor = "rgb(0, 100, 0)";
document.getElementById("idc").style.color = "rgb(255, 255, 255)";
document.getElementById("title").innerHTML = "Agri Industries";
} else {
return;
}
}
if (id = "ict") {
if (document.getElementById("ictContent").style.display = "none") {
document.getElementById("ictContent").style.display = "block";
// Hide other content
document.getElementById("agriContent").style.display = "none";
document.getElementById("introContent").style.display = "none";
// Change the look of the title
document.getElementById("idc").style.backgroundColor = "rgb(36, 46, 111)";
document.getElementById("idc").style.color = "rgb(255, 255, 255)";
document.getElementById("title").innerHTML = "ICT Investments";
} else {
return;
}
}
if (id = "intro") {
if (document.getElementById("introContent").style.display = "none") {
document.getElementById("introContent").style.display = "block";
// Hide other content
document.getElementById("agriContent").style.display = "none";
document.getElementById("ictContent").style.display = "none";
// Change the look of the title
document.getElementById("idc").style.backgroundColor = "rgb(255, 255, 255)";
document.getElementById("idc").style.color = "rgb(0, 0, 0)";
document.getElementById("title").innerHTML = "Introduction";
} else {
return;
}
}
}
</script>
JavaScript的不点火。
我知道这不是最优雅的(或必然有效的)做法,所以如果任何人都可以提出一个更好的方法,我全是耳朵。
现在虽然,我只是非常喜欢这个工作,但我看不出问题所在。
第一个'if'语句总是评估为'true',所以我相信你是正确的。 – ChaosPandion 2010-09-27 13:50:09
O.o就是这么简单......我以为我记得有关'=='的一些东西只是不确定,非常感谢:) – 2010-09-27 13:50:48
好的。此外,我建议重构代码以减少所有冗余document.getElementById()的处处,即帮助可读性。此外,没有其他需要{return; }如果海报做“其他如果”而不是“如果”。 – Paul 2010-09-27 13:51:30