Jquery UI Tabs add class

独自空忆成欢 提交于 2019-12-13 01:28:11

问题


Below is my HTML set up for the tabs which all works fine, i want to extend it a bit by changing the class of the outer containing div (id=tab-wrap class=bike) to whatever tab is showing. The idea is to change the background image based on the tab.

    <div id="tab-wrap" class="bike">

        <div id="batter-finder">
            <h1>Battery Finder</h1>
            <p>Choose Your Application then find your battery</p>

            <div id="tabs">
               <ul>
                  <li><a href="#bike">Bike</a></li>
                  <li><a href="#atv">ATV</a></li>
                  <li><a href="#utv">UTV</a></li>
                  <li><a href="#snow">Snow</a></li>
                  <li><a href="#water">Water</a></li>
               </ul>

               <div id="bike">
                  <p>Bike content</p>
               </div>

               <div id="atv">
                  <p>ATV content</p>
               </div>

               <div id="utv">
                  <p>UTV content</p>
               </div>

               <div id="snow">
                  <p>Snow content</p>
               </div>

               <div id="water">
                  <p>Water content</p>
               </div>

            </div> <!-- /tabs -->

        </div>

    </div>

So if i select the tab "water" i'd like to change the class from:

<div id="tab-wrap" class="bike">

to

<div id="tab-wrap" class="water">

An elegant fade in/out change would be good. My jquery is as follows

$(document).ready(function() {

    $("#tabs").tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
});

Appreciate the help Thanks


回答1:


you can do it like this,

demo

$("#tabs").tabs({ 
    fx: { height: 'toggle', opacity: 'toggle' },
    select: function(event, ui) {
         $("#tab-wrap").attr('class', ui.panel.id);
    }
});



回答2:


$("#tabs").bind("tabsselect", function(event, ui) {
    $("#tab-wrap").attr('class', $(ui.panel).attr('id'));
}

or if you want to bind the event when you declare the tabs:

$("#tabs").tabs({
    select: function(event, ui) {
        $("#tab-wrap").attr('class', $(ui.panel).attr('id'));
    }
});

Check out the events section of the jQuery UI tabs demo page.



来源:https://stackoverflow.com/questions/3271235/jquery-ui-tabs-add-class

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