jQuery how to set first tab active?

痴心易碎 提交于 2019-12-20 03:33:11

问题


 $( '#xxx' ).tabs({
select: function(event, ui) { 
var theSelectedTab2 = ui.index;

if (theSelectedTab2  == 0) {
$('ul li.ep_s1').removeClass('ep_s1').addClass('ep_s-click1');
if ($('ul li#h13').hasClass('ep_l-click1')) {
   $('ul li#h13').removeClass('ep_l-click1').addClass('ep_l1');
}}
else if (theSelectedTab2  == 1 ) {
$('ul li.ep_l1').removeClass('ep_l1').addClass('ep_l-click1');
if ($('ul li#h12').hasClass('ep_sidebar_friends-click1')) {
   $('ul li#h12').removeClass('ep_s-click1').addClass('ep_s1');
}}
 }
}); 

if i use selected: 0 it will set up the first tab as active but it will now go through the if (theSelectedTab2 == 0) statement and go through adding those classes.

if after the page loads i click on those tabs the script works perfect.

basically i want when the page loads the if (theSelectedTab2 == 0) statement and everything that happens inside it to be active.

thanks


回答1:


Trigger a click event on the first tab after page load?

Like $('first tab selector').click()




回答2:


Perhaps the following line, after the tabs initialize:

$( '#xxx' ).tabs('select', 0);

Hopefully, it will also trigger the event you want.




回答3:


I had to do like this to make it work:

jQuery( "#xxx" ).tabs({active: 1});
jQuery( "#xxx" ).tabs("option", "active", 0);

Doing this way trigger the events soon enough




回答4:


that's what I'd do

$(function () {
    $("ul.nav-tabs li:first").addClass("active");
    $(".tab-content .tab-pane:first").addClass("active");
});


来源:https://stackoverflow.com/questions/5624486/jquery-how-to-set-first-tab-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!