简体   繁体   English

jQuery UI标签:在创建时获取嵌套标签的宽度

[英]jquery ui tabs: get width of nested tabs on creation

I have Master tab element with tab1 and tab2 . 我有带有tab1tab2主选项卡元素。 Where tab2 has another nested tab elements with subtab1 and subtab2 . 其中tab2还有另一个嵌套的tab元素,分别带有subtab1subtab2

Initially master tab has only tab1 .Then tab2 , tab3 and so on .. are created dynamically and have subtab1 and subtab2 . 最初,master选项卡仅具有tab1然后动态创建tab2tab3等..并具有subtab1subtab2

GOAL: I want to get width of nested tabs ( subtabs-* ) when they are created.Without having to select them or use any nasty events. 目标:我想在创建嵌套选项卡( subtabs-* )时获得其宽度。不必选择它们或使用任何讨厌的事件。

PROBLEM: I can get width of Mater tab with var master_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$master).width(); 问题:我可以使用var master_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$master).width();获得Mater标签的宽度var master_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$master).width(); But i can not get width of slave tab . 但是我无法获得slave tab的宽度。

my guess is becuase when tab2 is created but not selected yet and hence subtab-* are not visible and thus width is zero. 我的猜测是因为创建了tab2却尚未选择它,因此subtab- *不可见,因此宽度为零。 But subtabs have been created so there must be a way to get its width. 但是已经创建了子选项卡,因此必须有一种获取其宽度的方法。

I was able to get widht of slave tab via atcive:event. 我可以通过atcive:event获得从属选项卡的信息。 But this is not my requirement. 但这不是我的要求。 I need to get its width when it is created. 创建它时,我需要获取它的宽度。

在此处输入图片说明

JSFIDDLE : HERE JSFIDDLE此处

HTML: HTML:

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">tab1</a></li>
    <li><a href="#tabs-2">tab2</a></li>
  </ul>
  <div id="tabs-1">
    some data
  </div>
  <div id="tabs-2">    
        <div id="subtabs">
          <ul>
            <li><a href="#subtabs-1">subtab1</a></li>
            <li><a href="#subtabs-2">subtab2</a></li>
          </ul>
          <div id="subtabs-1">
            some data
          </div>
          <div id="subtabs-2">
            some data
          </div>
        </div>    
  </div>
</div>

JS: JS:

var $master = $("#tabs").tabs();
var $slave = $("#subtabs").tabs();

var master_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$master).width();
// For display purpose append to body or use console.log
$("body").append("Master : "+master_width+"<br>");
var slave_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$slave).width();
// For display purpose append to body or use console.log
$("body").append("Slave : "+slave_width);//returns zero

I don't think you can, the width isn't known until it's shown. 我不认为可以,宽度直到显示出来才知道。 There are some ways round this (see this answer: jQuery - Get Width of Element when Not Visible (Display: None) ). 有一些方法可以解决此问题(请参见此答案: jQuery-在不可见时获取元素的宽度(显示:无) )。

But the easiest is to just select the tab and get the width, if you unselect it afterwards it shouldn't be visible to the user (although that may depend on the system/browser). 但是最简单的方法就是选择选项卡并获取宽度,如果此后取消选择它,则用户将看不到它(尽管这可能取决于系统/浏览器)。 This gets the length without any flicker on my system: 这得到的长度在我的系统上没有任何闪烁:

// Activate new tab 
var oldActive = $( "#tabs" ).tabs( "option", "active" );
$( "#tabs" ).tabs( "option", "active", 1 );

// Get the width
var slave_width = $("div.ui-tabs-panel:not(.ui-tabs-hide)",$slave).width();           
$("body").append("Slave : "+slave_width);

// Make the original tab active 
$( "#tabs" ).tabs( "option", "active",oldActive );

Fiddle (sorry for the ugly code to add the second tab) 小提琴 (很抱歉添加第二个标签的丑陋代码)

Otherwise you might need to look at the answer I linked above which has lots of generic options. 否则,您可能需要查看我上面链接的答案,该答案具有很多通用选项。 I would wonder if you can create the subtabs 'under' the top tabs (and so hidden), measure them and then add them to the second tab. 我想知道您是否可以在顶部选项卡“下”(然后隐藏)创建子选项卡,对其进行度量,然后将其添加到第二个选项卡。

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

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