使用外部超链接激活CSS选项卡

时间:2015-12-05 11:45:49

标签: javascript jquery css

我想在我的项目中使用PURE CSS TABS标签,但不知道如何使用超链接选择标签:请在此处查看我的代码笔:

<a href="#specs">Select Speccification</a>

<ul class="tabs">
  <li>
    <input type="radio" name="tabs" id="tab1" checked />
    <label for="tab1">Description</label>
    <div id="tab-content1" class="tab-content">
      <p>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>
    </div>
  </li>

  <li>
    <a id="specs"></a>
    <input type="radio" name="tabs" id="tab2" />
    <label for="tab2">Specification</label>
    <div id="tab-content2" class="tab-content">
      <p>"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla?</p>
    </div>
  </li>
</ul>

<br style="clear: both;" />

Codepen

如果用户单击选择规范或显示规范按钮,我想要实现的目标应该激活规范选项卡。

我认为它可以通过javascript实现,但我不知道如何。请帮忙。

3 个答案:

答案 0 :(得分:1)

这是jQuery版本,所以不要忘记将jquery.js链接到您的文件 将此功能添加到js文件中。

function changeTab(){
  $('#tab2').attr('checked', 'checked');
}

然后改变这个:

<a href="#specs">Select Speccification</a>

到此:

<a href="#" onclick="changeTab();">Select Speccification</a>

答案 1 :(得分:0)

<a href="javascript:;" id="tab1">Tab1</a>

<div id="tabContent1">
    Tab1 Content
</div>

<script>
  $(document).ready(function(){
     $('#tab1').on('click', function(){
         $('#tabContent1').show();   
      })

  })
</script>

在CSS中,默认情况下隐藏tabContent1的内容 您必须在脚本中包含jquery库。

Codepen example

答案 2 :(得分:0)

此外,您可以使用:target选择器在纯CSS中执行此操作 http://www.w3schools.com/cssref/sel_target.asp

<style>
    .tab {
        display: none;
    }

    .tab:target {
        display: block;
    }
</style>

<div class="panel">
    <a href="#tab-1">tab 1 button</a>
    <a href="#tab-2">tab 2 button</a>
    <a href="#tab-3">tab 3 button</a>
</div>

<div class="tabs">
    <div id="tab-1" class="tab">tab 1 content</div>
    <div id="tab-2" class="tab">tab 2 content</div>
    <div id="tab-3" class="tab">tab 3 content</div>
</div>