JqueryUI 1.11 tabs link directly to tab while on the same page

偶尔善良 提交于 2019-12-08 20:50:36

Unfortunately the tabs widget doesn't do this automatically. You have to listen for hashchange events, and manually set the active tab. Here's an approach that might work for you:

$('#tabs').tabs({
    create: function() {
        var widget = $(this).data('ui-tabs');
        $(window).on('hashchange', function() {
            widget.option('active', widget._getIndex(location.hash));
        });
    }
});

The above code will provide a handler for the create event. Then, we get the actually widget instance using data(). Next, we setup a handler for the hashchange event. This is triggered whenever one of our external links is clicked. This handler will set the active tab based on the current location.hash value.

We're using _getIndex(), an internal tabs method to figure out the index of the active tab, based on the hash value. Despite being an internal method, it's a good shortcut in this context.

Here's an example.

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