简体   繁体   English

jQuery垂直选项卡-创建指向选项卡的附加链接

[英]JQuery Vertical Tabs - create additional link to tab

I'm using this script for jQuery Vertical Tabs. 我正在将此脚本用于jQuery Vertical Tabs。

http://www.scriptbreaker.com/javascript/script/JQuery-vertical-tab-menu http://www.scriptbreaker.com/javascript/script/JQuery-vertical-tab-menu

<script language="JavaScript">
$(document).ready(function() {

$(".tabs .tab[id^=tab_menu]").hover(function() {
    var curMenu=$(this);
    $(".tabs .tab[id^=tab_menu]").removeClass("selected");
    curMenu.addClass("selected");

    var index=curMenu.attr("id").split("tab_menu_")[1];
    $(".curvedContainer .tabcontent").css("display","none");
    $(".curvedContainer #tab_content_"+index).css("display","block");
});
});
</script>

Here's the HTML for the tabs (links) 这是标签(链接)的HTML

<div class="tabscontainer">
 <div class="tabs">
     <div class="tab selected first" id="tab_menu_1">
         <div class="link">JavaScript</div>
         <div class="arrow"></div>
     </div>
     <div class="tab" id="tab_menu_2">
         <div class="link">CSS</div>
         <div class="arrow"></div>
     </div>
      <div class="tab last" id="tab_menu_3">
         <div class="link">JQuery</div>
         <div class="arrow"></div>
     </div>
</div>

I am trying to create an additional link from within a tabs content to another tab. 我正在尝试从选项卡内容中创建到另一个选项卡的附加链接。

So this way I can link to the next tab or the last tab, keeping the same functionality as the current tab links (no refresh) 这样,我可以链接到下一个标签或最后一个标签,并保持与当前标签链接相同的功能(不刷新)

This is my first question on stackoverflow and I've searched far and wide. 这是我关于stackoverflow的第一个问题,已经进行了广泛搜索。

I really appreciate all the help. 我真的很感谢所有帮助。 This is a great community. 这是一个很棒的社区。

UPDATE: 更新:

Okay, Here is an edit with my full code: 好的,这是我的完整代码的编辑内容:

<script type="text/javascript">
$(document).ready(function() {

$(".tabs .tab[id^=tab_menu]").click(function() {
    var curMenu=$(this);
    $(".tabs .tab[id^=tab_menu]").removeClass("selected");
    curMenu.addClass("selected");

    var index=curMenu.attr("id").split("tab_menu_")[1];
    $(".curvedContainer .tabcontent").css("display","none");
    $(".curvedContainer #tab_content_"+index).css("display","block");

});

});
</script>

<div class="curvedContainer">       

<div class="tabcontent" id="tab_content_1" style="display:block">
<p>text</p> 
</div>

<div class="tabcontent" id="tab_content_2">
<p>text</p>
</div>

<div class="tabcontent" id="tab_content_3">
<p>text</p>
</div>

<div class="tabcontent" id="tab_content_4"><h2 class="wwd_title">
<p>text</p> 
</div>  

</div>

<div class="tabscontainer">

<div class="tabs">         
<div class="tab selected first" id="tab_menu_1">             
<div class="link">Onefish</div>       
</div>         
<div class="tab" id="tab_menu_2">             
<div class="link">Twofish</div>                   
</div>         
<div class="tab" id="tab_menu_3">             
<div class="link">Redfish</div>         
</div>          
<div class="tab last" id="tab_menu_4">             
<div class="link">Bluefish</div>                     
</div>    
</div>  

In order to make links in the tab contents force the tabs to switch, it helps to understand what is happening under the hood to make them switch by default. 为了使选项卡内容中的链接强制切换选项卡,这有助于了解幕后情况,以使默认情况下它们切换。

By default, mouseover events force the tabs to change; 默认情况下,鼠标悬停事件会强制更改选项卡。 therefore, the solution is to override the default click event of the hyperlinks in question by binding an event to hyperlinks in the tab content: 因此,解决方案是通过将事件绑定到选项卡内容中的超链接来覆盖有问题的超链接的默认click事件:

Include this code after the JavaScript code you showed in your question, inside the $(document).ready : 在问题中显示的JavaScript代码之后,在$(document).ready内包含此代码

$('.tabcontent').find('a').click(function() {
    event.preventDefault();
    $('.tabs #' + $(this).attr("rel")).mouseover();
});

The event.preventDefault() prevents the hyperlinks from attempting to redirect to the page entered in the href attribute, and the rel attribute is used as a way to bind the hyperlink to the specific tab being linked to. event.preventDefault()防止超链接尝试重定向到href属性中输入的页面,而rel属性用作将超链接绑定到要链接到的特定选项卡的方式。

The rel attribute is used to get the value of the id of the tab so we can programmatically invoke its mouseover event, and switch tabs. rel属性用于获取选项卡ID的值,因此我们可以以编程方式调用其mouseover事件,并切换选项卡。

<div class="tabscontainer">
 <div class="tabs">
     <div class="tab selected first" id="tab_menu_1">
         <div class="link">JavaScript</div>
         <div class="arrow"></div>
     </div>
     <div class="tab" id="tab_menu_2">
         <div class="link">CSS</div>
         <div class="arrow"></div>
     </div>
      <div class="tab last" id="tab_menu_3">
         <div class="link">JQuery</div>
         <div class="arrow"></div>
     </div>          
</div>
    <div class="curvedContainer">
        <div class="tabcontent" id="tab_content_1" style="display:block">content for tab 1 <a href="#" rel="tab_menu_2"> tab2</a></div>
        <div class="tabcontent" id="tab_content_2">content for tab 2</div>
        <div class="tabcontent" id="tab_content_3">content for tab 3</div>
    </div>

In the HTML content for your tab, simply include the hyperlink as you normally would, using an href="#" and a rel attribute. 在选项卡的HTML内容中,只需像平常一样使用href =“#”和rel属性来包含超链接。 The rel attribute must equal the id of the target tab, in this case, tab_menu_2. rel属性必须等于目标选项卡的ID,在这种情况下为tab_menu_2。

It Works as per your requirements 根据您的要求工作

Just add 只需添加

        <div class="tabs">
             <div class="tab selected first" id="tab_menu_1">
                 <div class="link">JavaScript</div>
                 <div class="arrow"></div>
             </div>
        </div>

in any of the tab 在任何标签中

Go to this link http://jsfiddle.net/ipsjolly/ssR5f/ 转到此链接http://jsfiddle.net/ipsjolly/ssR5f/

There is a similar tab in 3rd tab through which we can jump to 1st tab. 3rd标签中有一个类似的标签,通过它我们可以跳到1st标签。

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

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