Jquery Adding Class On Selected Tab
I am a beginner with jQuery and I am trying to modify a script. The script demo is here. Right now, they add a line above the selected tab, which I don't want. What I want to do
Solution 1:
the_tabs.click(function(e){
var element = $(this);
if( element.hasClass('active') ) {
returnfalse;
}
else {
the_tabs.removeClass('active');
element.addClass('active');
}
}
Solution 2:
You just need:
$(this).addClass('nameOfYourClass')
to add a class to the clicked object
Solution 3:
instead of
if(element.find('.active').length) returnfalse;
use
if(element.hasClass('.active')) returnfalse;
or you have to use .filter
instead of .find
, find is trying to find a child node that has the class .active but .filter
filters out the collection to find the matching css-selecotr
Solution 4:
Try this:
the_tabs.click(function(e){
/* "this" points to the clicked tab hyperlink: */var element = $(this);
/* If it is currently active, return false and exit: */if(element.hasClass('active')) returnfalse;
/* Detecting the color of the tab (it was added to the class attribute in the loop above): */var bg = element.attr('class').replace('tab ','');
$('ul.tabContainer .active').removeClass('active');
element.addClass('active');
/* Checking whether the AJAX fetched page has been cached: */if(!element.data('cache'))
{
/* If no cache is present, show the gif preloader and run an AJAX request: */
$('#contentHolder').html('<img src="img/ajax_preloader.gif" width="64" height="64" class="preloader" />');
$.get(element.data('page'),function(msg){
$('#contentHolder').html(msg);
/* After page was received, add it to the cache for the current hyperlink: */
element.data('cache',msg);
});
}
else $('#contentHolder').html(element.data('cache'));
e.preventDefault();
})
Also check my blog (website) for some nice jQuery guides. :)
Post a Comment for "Jquery Adding Class On Selected Tab"