jquery - Javascript Menu Setting Class to Active -
jquery - Javascript Menu Setting Class to Active -
i got javascript, html , css few days ago, i'm quite new it.... anyway, i'm trying create vertical menu twitter's bootstrap. i've managed create menu fine, want create when click on button on menu, create button have "active" class, tried best it, works, if select menu options in order, here code , if can help me prepare up, that'd great!
class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html> <head> <link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css"> <link rel="stylesheet" href="stylehelp.css"> <link href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/shift.css" rel="stylesheet"> <link href='http://fonts.googleapis.com/css?family=indie+flower' rel='stylesheet' type='text/css'> </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> <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> <div class="container1 col-md-10"> <ul class="nav nav-pills nav-stacked col-md-10"> <li class="active" id="gotclass" onclick="navigationswitch1()"><a href="#">test</a></li> <li class="placeholder" id="noclass1" onclick="navigationswitch2()"><a href="#">test1</a></li> <li class="placeholder" id="noclass2" onclick="navigationswitch3()"><a href="#">test2</a></li> </ul> </div> <div class="text"> <div class="container"> <p id="oogabooga">this simple text</p> </div> </div> </body> </html>
notice usage of this
within navswitch first argument. it's matter of getting logic work.
in onclick handler onclick="navswitch(this)"
this
refers current element called nodes. argument navswitch
named node
. understand usage of this
you'll need study object oriented programming
. google search it.
you wanted know setting of current
.
current ever element
lastly clicked on. reasoning ever lastly element available 1 have active class
unset.
being element clicked on active
1 set active, before we set current
clicked element
.
as far why chose gotclass
element. set arbitrarily because that's 1 had set active in first place. if alter in future can add together gotclass
class other element first 1 active
long have active
class set on it.
in short it's order in utilize variables, , assigned values. mess variable assigning different elements see happens.
<!doctype html> <html> <head> <link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css"> <link rel="stylesheet" href="stylehelp.css"> <link href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/shift.css" rel="stylesheet"> <link href='http://fonts.googleapis.com/css?family=indie+flower' rel='stylesheet' type='text/css'> </head> <body> <script> var current = null; function navswitch(node){ if(current === null){ current = document.getelementbyid("gotclass"); } current.classname = "placeholder"; current = node; current.classname = "active"; } </script> <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> <div class="container1 col-md-10"> <ul class="nav nav-pills nav-stacked col-md-10" id="nav0"> <li class="active" id="gotclass" onclick="navswitch(this)"><a href="#">test</a></li> <li class="placeholder" id="noclass1" onclick="navswitch(this)"><a href="#">test1</a></li> <li class="placeholder" id="noclass2" onclick="navswitch(this)"><a href="#">test2</a></li> </ul> </div> <div class="text"> <div class="container"> <p id="oogabooga">this simple text</p> </div> </div> </body> </html>
javascript jquery html css twitter-bootstrap
Comments
Post a Comment