前幾天我只使用Javascript,HTML和CSS,所以我對它很陌生....無論如何,我正在嘗試使用Twitter的Bootstrap製作垂直菜單。我設法讓菜單很好,但是我想在菜單上點擊一個按鈕時,它會使按鈕具有「活動」類,我盡我所能去做,它可以工作,但只有當您按順序選擇菜單選項時,這裏是我的代碼,如果有人能幫我解決這個問題,那太棒了!Javascript菜單設置類爲活動
<!DOCTYPE html>
<html>
<head>
\t <link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css">
\t <link rel="stylesheet" href="stylehelp.css">
\t <link href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/shift.css" rel="stylesheet">
\t <link href='http://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
\t </head>
<body>
<script>
function navigationSwitch1(){
document.getElementById("oogabooga").innerHTML = "It works.";
document.getElementById("gotclass").className = "placeholder"
document.getElementById("gotclass").id = "noclass2"
document.getElementById("noclass").className = "active gotclass";
document.getElementById("noclass").id = "gotclass";
}
function navigationSwitch2(){
document.getElementById("oogabooga").innerHTML = "It works.";
document.getElementById("gotclass").className = "placeholder"
document.getElementById("gotclass").id = "noclass"
document.getElementById("noclass1").className = "active gotclass";
document.getElementById("noclass1").id = "gotclass";
}
function navigationSwitch3(){
document.getElementById("oogabooga").innerHTML = "It works.";
document.getElementById("gotclass").className = "placeholder"
document.getElementById("gotclass").id = "noclass1"
document.getElementById("noclass2").className = "active gotclass";
document.getElementById("noclass2").id = "gotclass";
}
</script>
\t <title>Help</title>
<div class="nav">
<div class="container">
<ul class="pull-left nav nav-pills">
<li><a href="index.html">Home</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="cars.html">Cars</a></li>
</ul>
<ul class="pull-right nav nav-pills">
<li><a href="customerservice.html">Customer Care</a></li>
<li class="active"><a href="help.html">Help</a></li>
</ul>
</div>
</div>
\t <div class="container1 col-md-10">
\t \t <ul class="nav nav-pills nav-stacked col-md-10">
\t \t \t <li class="active" id="gotclass" onclick="navigationSwitch1()"><a href="#">Test</a></li>
\t \t \t <li class="placeholder" id="noclass1" onclick="navigationSwitch2()"><a href="#">Test1</a></li>
\t \t \t <li class="placeholder" id="noclass2" onclick="navigationSwitch3()"><a href="#">Test2</a></li>
\t \t </ul>
\t </div>
<div class="text">
\t <div class="container">
\t \t <p id="oogabooga">This is some simple text</p>
\t </div>
</div>
\t </body>
</html>
嘿,嘗試了這一點,它的工作就像一個魅力!但是我想了解這段代碼的作用,以便將來可以使用它!從我讀到的你有一個叫做current的變量,你創建一個名爲navSwitch的函數,這個節點是什麼意思?那麼你檢查如果當前等於null,它是,然後你將它設置爲getElementById gotclass,那麼你說設置當前的類名爲佔位符,當前的節點和當前的類名爲活動?你能解釋一下嗎?我很困惑哈哈 – user3910313 2014-10-18 15:17:13
給我一些時間我會詳細解釋。 – 2014-10-18 15:18:57
這很好,如果你感到困惑。當我學習它時,這些東西也讓我感到困惑。 – 2014-10-18 15:30:45