我並不是一個JavaScript程序員,但我已經想出了一種創建選項卡區域的方法(即單擊一個選項卡顯示不同的DIV內容),但我的代碼看起來非常龐大。想知道是否有人可以啓發我如何濃縮一些。凝聚javascript
這裏的HTML:
<aside id="sb-popular">
<p class="sb-popular-nav">
<a id="sbpt1" class="current" href="javascript:showdiv('sbp-latest'); hidediv('sbp-commented'); hidediv('sbp-popular'); hidediv('sbp-views'); tabset('sbpt1'); taboff('sbpt2'); taboff('sbpt3'); taboff('sbpt4');">Latest</a>
<a id="sbpt2" href="javascript:showdiv('sbp-commented'); hidediv('sbp-latest'); hidediv('sbp-popular'); hidediv('sbp-views'); tabset('sbpt2'); taboff('sbpt1'); taboff('sbpt3'); taboff('sbpt4');">Commented</a>
<a id="sbpt3" href="javascript:showdiv('sbp-popular'); hidediv('sbp-commented'); hidediv('sbp-latest'); hidediv('sbp-views'); tabset('sbpt3'); taboff('sbpt1'); taboff('sbpt2'); taboff('sbpt4');">Popular</a>
<a id="sbpt4" href="javascript:showdiv('sbp-views'); hidediv('sbp-commented'); hidediv('sbp-popular'); hidediv('sbp-latest'); tabset('sbpt4'); taboff('sbpt2'); taboff('sbpt3'); taboff('sbpt1');">Views</a>
</p>
<div id="sbp-latest">
Content here
</div>
<div id="sbp-commented">
Content here
</div>
<div id="sbp-popular">
Content here
</div>
<div id="sbp-views">
Content here
</div>
</aside>
正如你所看到的JavaScript有一個有點笨拙。這是我的JS。
function hidediv(d) { document.getElementById(d).style.display = "none"; }
function showdiv(d) { document.getElementById(d).style.display = "block"; }
function tabset(d) { document.getElementById(d).style.color = "white";}
function taboff(d) { document.getElementById(d).style.color = "black";}
你想使用諸如例如JavaScript框架分配在頭部的onclick jQuery的? – Erwin