0

I have two tabs, below is the HTML code for both:

<input type="button" value="Activos" id="tab-active" class="active">
<input type="button" value="Inactivos" id="tab-inactive">

And I create this code to change between tabs:

$("#tab-inactive").click(function() {
    var current = $(this);

    $("#tab-active").removeClass("active");
    current.addClass("active");

    $("#layout-center").load(Routing.generate('inactive_product'));
});

$("#tab-active").click(function() {
    var current = $(this);

    $("#tab-inactive").removeClass("active");
    current.addClass("active");

    $("#layout-center").load(Routing.generate('product_list'));
});

The code works but I'll like to know if exists a better way/path to do this, any improvements or something around this in order to optimize and minimize the code

4

2 回答 2

1

稍微修改一下上面的代码。

HTML

<button type="button" value="inactive_product" class="tab active">Activos</button>
<button type="button" value="product_list" class="tab">Inactivos</button>

查询

$('.tab').click(function() 
{
    var $this         = $(this);
    var $tabs         = $('.tab');
    var route         = $this.val();
    var $layoutCenter = $("#layout-center");        

    $tabs.removeClass('active');
    $this.addClass('active');

    $layoutCenter.load(Routing.generate(route));  
});
于 2013-09-02T19:50:17.897 回答
0

例如:

<input type="button" data-route="route1" value="Activos" class="tab active">
<input type="button" data-route="route2" value="Inactivos" class="tab">

$( ".tab" ).click(function() {
    var this$ = $( this );
    if ( !this$.hasClass( 'active' ) ) {
        $( '.tab.active' ).removeClass( 'active' );
        this$.addClass( 'active' );
        $( "#layout-center" ).load( Routing.generate( this$.data( 'route' ) ) );
    }
 } );

如果只有 2 个选项卡,甚至toggleClass( 'active' )可以使用。

于 2013-09-02T19:31:14.467 回答