3

jQuery Address プラグインを使用して、jQuery UI タブをブックマーク可能にし、戻るボタンの履歴に準拠させています。よく働く。

ただし、ネストされたタブに同じ種類の「ディープリンク」を提供できるかどうか疑問に思っていましたか? たとえば、次のものがあります。

<div id="tabs">
 <ul class="links">
  <li><a href="#one">main tab one</a></li>     
  <li><a href="#two">main tab two</a></li>
  <li><a href="#three">main tab three</a></li>
  <li><a href="#four">main tab four</a></li>
 </ul>
 <div id="one">  Main tab one content </div> 
 <div id="two">
  Main tab two content 
  <div id="nested">
   <ul>
    <li><a href="#nestedone">nested tab one</a></li>
    <li><a href="#nestedtwo">nested tab two</a></li>
    <li><a href="#nestedthree">nested tab three</a></li>
   </ul>
   <div id="nestedone"> nested tab one </div>
   <div id="nestedtwo"> nested tab two </div>
   <div id="nestedthree"> nested tab three </div>
  </div> <!-- end nested -->
 </div> 
 <div id="three">  Main tab three content </div> 
 <div id="fout">  Main tab four content </div> 
</div> <!-- end tabs -->


<script>
$(document).ready(function(){
   /* main tabs stuff */
   var $tabs = $("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
   $("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');

   /* nested tabs */
   var $nested = $("#nested").tabs().addClass('ui-tabs-hz');
   $("#nested ul").removeClass('ui-tabs-nav');

   /* show the hash's tab whenever the address is changed */
   $.address.change(function(event){               
      $("#tabs").tabs( "select" , window.location.hash ) ;  
   })

   /* when the tab is selected update the url with the hash */
   $("#tabs").bind("tabsselect", function(event, ui) {          
      window.location.hash = ui.tab.hash;                           
   })
});
</script>

すべてがメイン(外側のタブ)で完全に機能します:

  • タブの href リンクは URL に正しく配置されます。
  • 外側のタブはブラウザの戻るボタンと進むボタンに従います
  • 外側のタブはブックマーク可能です

ただし、内側のタブで同じタイプの機能を取得できないようです。

ネストされたタブが「メイン」タブの 1 つに含まれている場合、上記のバインド関数は $(this) を外側のタブとして認識します (ネストされたタブがクリックされた場合でも)。その結果、JavaScript コードはネストされたタブに対して正しく実行されますが、最後のメイン (外側) タブが選択されたタブであるかのように実行されます。

ネストされたタブが処理された後、最後のメイン (外側) タブが実行される前に、JavaScript の実行を停止する方法がわかりません。私が得ることができる最も近いのは、バインド関数に次を追加し、ネストされたタブを含む外側のタブで実行を停止することです。

   $("#tabs").bind("tabsselect", function(event, ui) {          
      if ($(ui.tab).closest('div').attr('id') !== "nested") {
         window.location.hash = ui.tab.hash; 
      }                     
   })

明らかな何かが欠けていると確信しています。助言がありますか?

ありがとう!

4

1 に答える 1

0

ハッシュを結合するには、おそらく何らかの回避策を講じる必要があります。.address プラグインの経験はあまりありませんが、次のようなものはどうでしょうか。

<div id="tabs">
 <ul class="links">
  <li><a href="#one">main tab one</a></li>     
  <li><a href="#two">main tab two</a></li>
  <li><a href="#three">main tab three</a></li>
  <li><a href="#four">main tab four</a></li>
 </ul>
 <div id="one">  Main tab one content </div> 
 <div id="two">
  Main tab two content 
  <div id="nested">
   <ul>
    <li><a href="#two-nestedone">nested tab one</a></li>
    <li><a href="#two-nestedtwo">nested tab two</a></li>
    <li><a href="#two-nestedtwo">nested tab three</a></li>
   </ul>
   <div id="two-nestedone"> nested tab one </div>
   <div id="two-nestedtwo"> nested tab two </div>
   <div id="two-nestedthree"> nested tab three </div>
  </div> <!-- end nested -->
 </div> 
 <div id="three">  Main tab three content </div> 
 <div id="four">  Main tab four content </div> 
</div> <!-- end tabs -->

そしてJavaScriptの場合:

$(document).ready(function(){
   /* main tabs stuff */
   var $tabs = $("#tabs").tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
   $("#tabs li").removeClass('ui-corner-top').addClass('ui-corner-left');

   /* nested tabs */
   var $nested = $("#nested").tabs().addClass('ui-tabs-hz');
   $("#nested ul").removeClass('ui-tabs-nav');

   /* show the hash's tab whenever the address is changed */
   $.address.change(function(event){               
      var arrParts = window.location.hash.substring(1).split("-");      
      $("#tabs").tabs( "select" , '#' + arrParts[0] ) ;
      if (arrParts.length > 1) {  
        $("#nested").tabs( "select" , '#' + arrParts[1] ) ;  
      }
   })

   /* when the tab is selected update the url with the hash */
   $("#tabs, #nested").bind("tabsselect", function(event, ui) {          
      window.location.hash = ui.tab.hash;                           
      event.stopPropagation();
   })
});
于 2011-12-10T00:36:49.027 に答える