简体   繁体   English

带链接的jquery选项卡记住最后一个活动项

[英]jquery tabs with links remember last active item

I did the following code, to be able to change the class of an li tag and then change the CSS of it. 我做了以下代码,以便能够更改li标签的类,然后更改它的CSS。
I created a cookie variable that help me to select the good li tag. 我创建了一个cookie变量,帮助我选择好的li标签。 but I have a gap when I click. 但是当我点击时,我有一个空白。
I have three tabs : device, links and sites. 我有三个标签:设备,链接和网站。
for example if I click on devices and had click on sites before, sites will be selected. 例如,如果我点击设备并之前点击过网站,则会选择网站。

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('.tabs .tab-links a').on('click', function(e)  {
    jQuery.cookie("select", jQuery(this).parent('li').attr('id'));    
});  
jQuery('#' + jQuery.cookie("select")).addClass('active').siblings().removeClass('active');
});
</script>
<div class="tabs" >
  <ul class="tab-links">
    <li id="device"><a href="/netmg/controller/device/search">Devices</a></li>
    <li id="link"><a href="/netmg/controller/link/search">Links</a></li>
    <li id="site"><a href="/netmg/controller/site/search">Sites</a></li>
  </ul>
</div>

Since clicking the link takes you to a new page, there is no need to do this programmatically. 由于单击链接会将您带到新页面,因此无需以编程方式执行此操作。 The page /netmg/controller/device/search should have 页面/ netmg / controller / device / search应具有

<li id="device" class="active">...</li>
<li id="link">...</li>
<li id="site">...</li>

The page /netmg/controller/link/search should have 页面/ netmg / controller / link / search应该有

<li id="device">...</li>
<li id="link" class="active">...</li>
<li id="site">...</li>

The page /netmg/controller/site/search should have 页面/ netmg / controller / site / search应具有

<li id="device">...</li>
<li id="link">...</li>
<li id="site" class="active">...</li>

You can put that in the HTML. 你可以把它放在HTML中。

If, instead, you want to make it all one page and the href attribute indicates what content is displayed, say, in a <div id="deviceContent"></div> or similar, you could use this: 相反,如果您希望将其全部设为一个页面并且href属性指示显示的内容,例如,在<div id="deviceContent"></div>或类似内容中,您可以使用:

<script>
jQuery(document).ready(function() {
    jQuery('.tab-links li a').click(function(e){
        e.preventDefault();
        var newdiv = jQuery(this).attr('href');
        jQuery('.tab-links li').removeClass('active');
        jQuery(this).parent('li').addClass('active');
        jQuery('.contents div').hide();
        jQuery(newdiv).show()
    });
});
</script>
<div class="tabs" >
  <ul class="tab-links">
    <li id="device" class="active"><a href="#deviceContent">Devices</a></li>
    <li id="link"><a href="#linkContent">Links</a></li>
    <li id="site"><a href="#siteContent">Sites</a></li>
  </ul>
</div>
<div class="contents">
    <div id="deviceContent"><!-- insert some contents -->a</div>
    <div id="linkContent"><!-- insert some contents --> b</div>
    <div id="siteContent"><!-- insert some contents -->c</div>
</div>

For a demo, see this fiddle . 对于演示,请看这个小提琴

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

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