【问题标题】:How to link navigation menu to jQuery tabs如何将导航菜单链接到 jQuery 选项卡
【发布时间】:2015-12-24 04:45:15
【问题描述】:

我有一个标题导航菜单,我需要链接到 jQuery 选项卡 - 因此链接必须在同一页面上打开一个选项卡并链接到选项卡并从另一个页面打开它们。我从另一个页面上得到它,但不知道如何在同一页面上打开带有外部链接的特定标签,而不包括每个子页面的单独标题,它使用导航中链接的标签。

这里是一个Jsfiddle,但是它有问题,因为导航中的链接指向外部文件。要点是,导航应该既可以从外部页面工作,也可以在同一页面上工作,只是为了打开一个标签

http://jsfiddle.net/vbonoL5b/

这是菜单的一部分(它的 2 级 ul 菜单)

<div id="nav">
  <ul><li><a href="index.php?lang=<?php echo $lang ?>">Home</a>
                    <ul>
                        <li><a href="index.php?lang=<?php echo $lang ?>#tabs-1" class="tabLink">Concept</a></li>
                        <li><a href="index.php?lang=<?php echo $lang ?>#tabs-2" class="tabLink">Benefits</a></li>
                    </ul>
  </li></ul>
</div>

这些选项卡是非常简单的 jQuery 选项卡:

<div id="icon-tabs">
              <ul>
                <li><a href="#tabs-1" id="item1" ><div class="icon-tab"><h4>Concept</h4><img src="img/concept.png"></img></div></a></li>
                <li><a href="#tabs-2" id="item2" ><div class="icon-tab"><h4>More</h4><img src="img/benefits.png"></img></div> </a></li>
              </ul>
        <div id="tabs-1">
           ....
        </div>
</div>

我尝试了一些方法,但始终无法在同一页面上打开链接:

$(function() {
//Load tabs
 $( "#icon-tabs" ).tabs();

//Check url and open appropriate tab (for when you come from external site)
 if(document.location.hash!='') {
    //get the index from URL hash
    tabSelect = document.location.hash.substr(1,document.location.hash.length);
    $("#icon-tabs").tabs('select',tabSelect-1);
 }  

//Use the nav to open local tabs?? I tried .onclic events based on ID and class of links in menu, but never got it working.
// For example:
 $('.tabLink').click(function(e) {
  if(document.location.hash!='') {
     (e).preventDefault();
     //get the index from URL hash
     tabSelect = document.location.hash.substr(1,document.location.hash.length);
     $("#icon-tabs").tabs('select',tabSelect-1);
   }
  });
});

我不确定我做错了什么。任何帮助或建议表示赞赏!

【问题讨论】:

  • 能否提供示例链接?
  • 当然,对不起,请给我分钟,我只在本地拥有它
  • 好吧,因为#nav 菜单在链接中使用了外部文件,所以我无法让它全部工作:jsfiddle.net/vbonoL5b
  • 小提琴不工作。在这里你想做什么?在导航点击页面将重新加载 has 和重新加载后基于 #data 。相关选项卡将被选中?
  • 是的,.. 类似的东西

标签: javascript jquery html css


【解决方案1】:

使用下面的代码。 index 函数使用“ID”属性直接为您提供元素索引

$(function() {
//Load tabs
  $( "#icon-tabs" ).tabs();

  //Check url and open appropriate tab (for when you come from external site)
    if(document.location.hash!='') {
   //get the index from URL hash

     tabSelect = $('#icon-tabs div').index($(document.location.hash));
     $("#icon-tabs").tabs('select',tabSelect);       }  

    //Use the nav to open local tabs?? I tried .onclic events based on ID and class of links in menu, but never got it working.
  // For example:
  $('.tabLink').click(function(e) {
   if(document.location.hash!='') {
      e.preventDefault();
     tabSelect = $('#icon-tabs div').index($(document.location.hash));
     $("#icon-tabs").tabs('select',tabSelect);
    }
  });

});

【讨论】:

  • 我已经尝试过了,但似乎 .click 函数从未注册 - url 更改,但代码似乎没有执行
  • 你在 URL 中传递了什么? .标签的id?
  • 对不起,我不得不意外离开。基本上我正在尝试做的事情:如果您在另一个子页面上,那么导航中的 .tabLink 链接应该将您带到正确的子页面并选择正确的选项卡(这有效),但是一旦您在正确的页面上,然后他们应该只打开正确的标签。 - 所以链接应该适用于外部和内部链接。我希望我解释正确。我尝试了上述方法并查看了您发布的 jsfiddle,但我仍然无法使其正常工作 - 页面的 URL 更改,但选项卡未选择/打开。
  • 嗯,谢谢.. 在 JSFiddle 中工作得很好,但我无法让它在我的网站上工作。我会玩弄它并找出我做错了什么。还是非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多