Jquery UI选项卡更改Body Class

时间:2011-06-16 20:02:58

标签: javascript jquery css jquery-ui jquery-ui-tabs

我尝试过以下脚本无济于事。我在www.1tryten.com上有一系列标签。选项卡自动旋转并在鼠标悬停时更改。我正在努力让它当前活动的标签设置body标签的类。基本上,如果tab-1处于活动状态,那么body类将是tab-1等。这是否可行。

$(document).ready(function() {
$("#tabs").tabs({
    event: 'mouseover',
    fx: {
        opacity: 'toggle',
        duration: 'slow',
        show: function(event, ui) { $("body").addClass(ui.tab);}
    }
}).tabs('rotate', 5000, true);
});

以下是标签

的代码
<div id="tabs" class="tabs-bottom ui-tabs ui-widget ui-widget-content ui-corner-all"> 
    <div id="tabsone" class="tabsone ui-tabs-panel ui-widget-content ui-corner-bottom" style=""> 
    <img src="/images/mac-mini-banner.png">
        <h3>Mac Mini Security Mount.  New Mac Mini Mount.</h3>
</div> 
<div id="tabstwo" class="tabstwo ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide" style=""> 
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p> 
</div> 
<div id="tabsthree" class="tabsthree ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide" style=""> 
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p> 
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p> 
</div> 
            <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all"> 
                <li id="tabone" class="tabsone ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="#tabsone"><img src="/images/laptop-lock-tab-nav.png"></a></li> 
    <li id="tabtwo" class="tabstwo ui-state-default ui-corner-top"><a href="#tabstwo"><img src="/images/computer-lock-tab-nav.png"></a></li> 
    <li id="tabthree" class="tabsthree ui-state-default ui-corner-top"><a href="#tabsthree"><img src="/images/locker-tab-nav.png"></a></li> 
</ul> 
</div>

3 个答案:

答案 0 :(得分:1)

这是:

$("body").addClass(ui.tab);

尝试将ui.tab的类分配给body?因为addClass需要一个字符串作为其参数。你应该做的是:

$("body").attr('class', $(ui.tab).attr('class'));

...假设ui.tab是一个有效的对象。

答案 1 :(得分:0)

使用select callback:

$( ".selector" ).tabs({
   select: function(event, ui) { ... }
});

- EDIT从网站添加了代码 -

$("#tabs").tabs({
event: 'mouseover',
fx: {
    opacity: 'toggle',
    duration: 'slow'
},
show: function(event, ui) { 
    $("body").addClass(className);
}

});

更好的解释是:http://www.springenwerk.com/2008/03/jquery-ui-tabs-callbacks.html

答案 2 :(得分:-1)

您可以使用选择任何标签时触发的select事件。

$(document).ready(function() {
$("#tabs").tabs({
    event: 'mouseover',
    fx: {
        opacity: 'toggle',
        duration: 'slow',
        show: function(event, ui) { $("body").addClass(ui.tab);},

    },
    select: function(event, ui) { $("body").addClass($(ui.tab).attr('class'));}
}).tabs('rotate', 5000, true);
});