如何在 jQuery UI 选项卡中初始化选项卡式容器外部的选项卡式内容?

发布于 2024-12-03 10:52:05 字数 1248 浏览 0 评论 0原文

是否可能?如果可以,您将如何初始化选项卡容器元素之外的选项卡式内容?

该文档全部包含同一容器中的选项卡和选项卡内容,例如:

<div id="tabs">
    <ul>
        <li><a href="#tab1">Tab 1</a></li>
        <li><a href="#tab2">Tab 2</a></li>
        <li><a href="#tab3">Tab 3</a></li>
    </ul>
    <div id="tab1">
        Tab 1 content
    </div>
    <div id="tab2">
        Tab 2 content
    </div>
    <div id="tab3">
        Tab 3 content
    </div>
</div>

但是,对于我正在处理的项目,导航和选项卡内容不位于同一容器中,因此:

<nav id="tabs">
    <ul>
        <li><a href="#tab1">Tab 1</a></li>
        <li><a href="#tab2">Tab 2</a></li>
        <li><a href="#tab3">Tab 3</a></li>
    </ul>
</nav>
...
<article>
    <section id="tab1">
        Section 1 content
    </section>
    <section id="tab2">
        Section 2 content
    </section>
    <section id="tab3">
        Section 3 content
    </section>
</article>

Is it possible and if so, how would you initialize tabbed content outside of the tabs container element?

The documentation all includes the tabs and the tab content in the same container, example:

<div id="tabs">
    <ul>
        <li><a href="#tab1">Tab 1</a></li>
        <li><a href="#tab2">Tab 2</a></li>
        <li><a href="#tab3">Tab 3</a></li>
    </ul>
    <div id="tab1">
        Tab 1 content
    </div>
    <div id="tab2">
        Tab 2 content
    </div>
    <div id="tab3">
        Tab 3 content
    </div>
</div>

However, for the project I'm working on, the navigation and the tab content are not located within the same container, thus:

<nav id="tabs">
    <ul>
        <li><a href="#tab1">Tab 1</a></li>
        <li><a href="#tab2">Tab 2</a></li>
        <li><a href="#tab3">Tab 3</a></li>
    </ul>
</nav>
...
<article>
    <section id="tab1">
        Section 1 content
    </section>
    <section id="tab2">
        Section 2 content
    </section>
    <section id="tab3">
        Section 3 content
    </section>
</article>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜未央樱花落 2024-12-10 10:52:05

查看 tabs< 的源代码/code>(行 228233),似乎不可能这样做不需要某种“黑客”,比如像 William Niu 建议的那样克隆导航,或者包含两者tabs 的当前迭代会查找父元素内的选项卡面板;即它使用find()

但我仍然希望您可以使用通用容器,看起来您可以将导航(和面板)放置在容器内的任何位置(L215),并且您不必将容器放置在导航或面板旁边或附近,例如,实际的选项卡元素可以位于容器的深处。

更新:现在有一个官方方法可以做到这一点:http://bugs. jqueryui.com/ticket/7715

Looking at the source code of tabs (Lines 228 and 233), it seems impossible to do this without some sort of "hack" like cloning the navigation as William Niu suggests, or containing both the navigation and the panels inside a common container, etc. The current iteration of tabs looks for the tab panels inside the parent element; i.e. it uses find().

But I'm still hopeful that you can use a common container, it looks like you can place the navigation (and panels) anywhere inside the container (L215), and you don't have to place the container next to or near the navigation or panels, e.g. the actual tab elements can be deep within the container.

Update: Now there is an official way to do this: http://bugs.jqueryui.com/ticket/7715

与君绝 2024-12-10 10:52:05

我不知道这是否适合您,但一种解决方案是访问包含两者的元素。

例如

$( "#tabs").parent().tabs();

看看这个 fiddle

I don't know if this would work for you, but one solution would be to access an element that does contain both.

for example

$( "#tabs").parent().tabs();

Check out this fiddle

清泪尽 2024-12-10 10:52:05

如果您只是希望它对用户具有这种外观,您可以将导航栏克隆到其他位置并删除原始导航栏。

$('#tabs').tabs();

// clone the navigation bar and put it in the placeholder
var copy = $('#tabs').clone(true).attr('id', 'placeholder');
copy.find('div.ui-tabs-panel').remove();
$('#placeholder').replaceWith(copy);

// remove the original navigation bar
$('#tabs > ul').remove();

$('#placeholder li > a').click(function() {
    $(this).parent().siblings().removeClass('ui-tabs-selected ui-state-active');
});

查看实际操作:http://jsfiddle.net/william/QAJ2Z/1/

If you just want it to have that look to the user, you could clone the navigation bar to somewhere else and remove the original one.

$('#tabs').tabs();

// clone the navigation bar and put it in the placeholder
var copy = $('#tabs').clone(true).attr('id', 'placeholder');
copy.find('div.ui-tabs-panel').remove();
$('#placeholder').replaceWith(copy);

// remove the original navigation bar
$('#tabs > ul').remove();

$('#placeholder li > a').click(function() {
    $(this).parent().siblings().removeClass('ui-tabs-selected ui-state-active');
});

See this in action: http://jsfiddle.net/william/QAJ2Z/1/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文