jquery conflict with identical scripts
I am using jquery to load content into tabs, and switch the tabs on click. My problem is that one one page I am using this "tab switcher" twice, and it is causing a conflict. I am not too experienced with jquery, so my problem probably lies in the fact that I am creating the function twice in the head. Here is my jquery (you will notice that there are duplicate scripts, with the selectors changed up a bit so the "tab switchers" appear different.
<script type="text/javascript">
$(document).ready(function() {
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
开发者_JAVA百科 });
});
</script>
<script type="text/javascript">
$(document).ready(function() {
//When page loads...
$(".tabs_content").hide(); //Hide all content
$("ul.tab li:first").addClass("active").show(); //Activate first tab
$(".tabs_content:first").show(); //Show first tab content
//On Click Event
$("ul.tab li").click(function() {
$("ul.tab li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tabs_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
});
</script>
My css is all correct, I know the problem is above.The second script works fine, and the first script doesn't.
You can see this live here: link You will notice that the second script works fine (at the bottom : margie and todd. And the first script doesn't work (in the sidebar :categories and archives.)
Any idea how to fix this?
I know that script - I actually refactored it for another question. The code is pretty bad, in the sense that it contains many bad practices. Let's see what can be done here:
$(".tabs_content").not(':first-child').hide();
var tabs = $('.tabs li');
tabs.filter(':first-child').addClass('active');
tabs.click(function() {
var current = $(this);
if(!current.hasClass('active')){
current.addClass('active').siblings().removeClass('active');
var activeTab = current.find("a").attr("href");
current.parent().next().children().hide().filter(activeTab).fadeIn();
}
return false;
});
There - a single script for all your tabs. Rename all your tab containers to tabs
. This uses some pretty heavy chaining, which really isn't very efficient, but given the DOM here there isn't much to do. Use this so you won't need two scripts that do essentially the same thing.
See it working here: http://jsfiddle.net/E3SFt/2/. I copied your HTML character for character for this, with the minor modification to the class names as noted above. Also note that you've got some invalid HTML in there - li
elements inside div
s is not valid.
Edit: Stupid mistake, this.hasClass
should be current.hasClass
精彩评论