html - Connecting link item to content div id with jQuery -
i'm attempting create simple, multi-tabbed navigation utilizes single web page. have content laid out so:
html
<ul id="nav"> <li><a href="tab1">tab 1</a></li> <li><a href="tab2">tab 2</a></li> <li class="active"><a href="tab3">tab 3</a></li> </ul><!--/#nav--> <div id="content-box"> <div id="tab1"> <!--content tab1--> </div><!--/#tab1--> <div id="tab2"> <!--content tab2--> </div><!--/#tab2--> <div id="tab3"> <!--content tab3--> </div><!--/#tab3--> </div><!--/#content-box-->
jquery:
$("#nav li").on("click", function(event){ $("#nav li").removeclass("active"); $(this).addclass("active"); $("#content-box").children().hide(); });
my problem don't know how link li
class active
corresponding div
. of tab divs set hidden, , have class called .activeslide
turns sets visibility: visible
.
use html 5 data
attributes link div
html
<ul id="nav"> <li data-content="tab1">tab 1</li> <li data-content="tab2">tab 2</li> <li data-content="tab3" class="active">tab 3</li> </ul><!--/#nav-->
jquery
$("#nav li").on("click", function(event){ $("#nav li").removeclass("active"); $(this).addclass("active"); $("#content-box").children().hide(); $('#'+ $(this).data('content')).show(); //or $("#content-box").children().removeclass('activeslide'); $('#'+ $(this).data('content')).addclass('activeslide'); });
updated
add in ready function
$(document).ready(){ $("#content-box").children().hide(); $("#nav li").on("click", function(event){ ...... //above function goes here }); });
however recommned use jquery ui tab instead of reinventing wheel... :)
Comments
Post a Comment