JQuery selected tab problem?
I'm fairly new to JQuery and for some reason I can't get the currently selected tab to highlight correctly it should be a different color when selected. Can some one help me fix this problem?
Here is the JQuery.
$(document).ready(function() {
//When page loads...
$(".form-content").hide(); //Hide all content
$("#menu ul li:first").addClass("selected-link").show(); //Activate first tab
$(".form-content:first").show(); //Show first tab content
//On Click Event
$("#menu ul li").click(function() {
$("#menu ul li").removeClass("selected-link"); //Remove any "selected-link" class
$(this).addClass("selected-link"); //Add "selected-link" class to selected tab
$(".form-content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content
$(activeTab).fadeIn(); //Fade in the selected-link ID content
return false;
});
});
Here is the CSS.
#container {
width: 1024px;
background: blue;
padding: 0px;
margin: 0px;
float: left;
}
#menu {
padding: 0px;
margin: 0px;
}
#menu ul {
width: 1024px;
padding: 0px;
margin: 0px;
margin-top: 25px;
border: 0px;
float: left;
text-align: left;
list-style-type: none;
}
#menu li {
margin: 0px;
margin-right: 1px;
padding: 0px;
float: left;
border: 0px;
width: auto;
}
#menu a:link, #menu a:visited {
开发者_运维百科padding: 9px 9px;
float: left;
color: white;
text-decoration: none;
background: black;
width: auto;
}
#menu a.selected-link, #menu a:hover {
background: blue;
color: white;
}
Here is the HTML.
<div id="body-content">
<div id="menu">
<ul>
<li><a href="#tab-1" title="">tab 1</a></li>
<li><a href="#tab-2" title="">tab 2</a></li>
<li><a href="#tab-3" title="">tab 3</a></li>
<li><a href="#tab-4" title="">tab 4</a></li>
<li><a href="#tab-5" title="">tab 5</a></li>
</ul>
</div>
<div id="container">
<div id="tab-1" class="form-content">
<p>tab 1</p>
</div>
<div id="tab-2" class="form-content">
<p>tab 2</p>
</div>
<div id="tab-3" class="form-content">
<p>tab 3</p>
</div>
<div id="tab-4" class="form-content">
<p>tab 4</p>
</div>
<div id="tab-5" class="form-content">
<p>tab 5</p>
</div>
</div>
<div>
Try changing these lines:
$("#menu ul li").removeClass("selected-link");
$(this).addClass("selected-link");
To:
$("#menu ul li").find('a').removeClass("selected-link");
$(this).find('a').addClass("selected-link");
You want to add class to link
not li
.
Edit
$(document).ready(function() {
//When page loads...
$(".form-content").hide(); //Hide all content
var firstMenu = $("#menu ul li:first");
firstMenu.show();
firstMenu.find("a").addClass("selected-link"); //Activate first tab
$(".form-content:first").show(); //Show first tab content
//On Click Event
$("#menu ul li").click(function() {
$("#menu ul li a").removeClass("selected-link"); //Remove any "selected-link" class
$(this).find("a").addClass("selected-link"); //Add "selected-link" class to selected tab
$(".form-content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content
$(activeTab).fadeIn(); //Fade in the selected-link ID content
return false;
});
});
"
精彩评论