简体   繁体   English

jQuery UI Tabs-Cookie与选择刚刚添加的选项卡之间发生冲突?

[英]jQuery UI Tabs - conflict between cookie & select just added tab?

I am using jquery.cookies to keep a tab selected after refresh. 我正在使用jquery.cookies刷新后使选项卡保持选中状态。

I would also like that as soon as a new tab is created, it is selected. 我还希望在创建新标签后立即选择它。

Using jQuery UI instructions I currently have: 使用jQuery UI指令,我目前有:

    var cookieName = 'stickyTab';

    $(".tabs").tabs({
        fx: {
            opacity: 'toggle',
            duration: 'fast'
        },
        selected: ( $.cookies.get( cookieName ) || 0 ),
                    select: function( e, ui )
                {
                $.cookies.set( cookieName, ui.index );
                }
    });

    var $tabs = $('.tabs').tabs({
    add: function(event, ui) {
        $tabs.tabs('select', '#' + ui.panel.id);
    }
});

Unfortunately this doesn't work -- when I create a new tab, the one that was previously open remains selected (maybe because the cookie is overriding the select function? 不幸的是,这行不通-当我创建一个新标签时,以前打开的标签仍保持选中状态(也许是因为cookie覆盖了select函数?

My tabs are created via PHP POST and data is retrieved from DB and looped to create LI and DIV elements. 我的选项卡是通过PHP POST创建的,并且从DB检索数据并循环创建LIDIV元素。 (ie, my tabs are not created directly from JS). (即,我的标签页不是直接从JS创建的)。

Anyone have suggestions to resolve this? 有人建议解决此问题吗?

Thanks! 谢谢!

Your bug stems from the fact you're only updating your cookies via JavaScript, but are adding new tabs via PHP on page reloads. 您的错误源于您仅通过JavaScript更新cookie,而是在页面重新加载时通过PHP添加新标签的事实。 This results in new tabs being appended but not synced with your cookie. 这会导致附加新标签,但未与您的Cookie同步。

Since the jQuery UI Tabs selected property is an index. 由于jQuery UI Tabs selected属性是一个索引。 And you're tabs are created via an array, a simple: 标签是通过数组创建的,很简单:

setcookie('stickyTab',count($array)-1);
// this will set the selected tab to the last appended tab

Take note you only need to run this statement when new tabs are appended. 请注意,仅在附加新选项卡时才需要运行此语句。 Otherwise you will overwrite the cookie set via JS. 否则,您将覆盖通过JS设置的cookie。

If you have any more issues, be sure to comment and we'll try and fix. 如果您还有其他问题,请务必发表评论,我们将尝试解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM