嵌套超链接时,jQuery选项卡插件会创建额外的锚标记

时间:2014-09-26 19:48:27

标签: javascript jquery tabs anchor

我是JavaScript和jQuery的新手。在尝试将锚标签嵌套在选项卡插件的内容div中时,正在创建额外的空actor标签。如何调整代码以防止这种情况发生。我在这里创建了一个JSFiddle:http://jsfiddle.net/p94aybey/

由于

<ul class="tabs" data-tabgroup="first-tab-group">
  <li><a href="#group1-tab1" class="active">Tab 1</a></li>
  <li><a href="#group1-tab2">Tab 2</a></li>
  <li><a href="#group1-tab3">Tab 3</a></li>
</ul>
<section id="first-tab-group" class="tabgroup">
  <div id="group1-tab1">
    <h2>Heading 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  </div>
  <div id="group1-tab2">
    <h2>Heading 2</h2>
    <p>Adipisci autem obcaecati velit natus quos beatae explicabo.</p>
  </div>
  <div id="group1-tab3">
    <h2>Heading 3</h2>
    <p>Atque ratione soluta laboriosam illo inventore amet?</p>

    <a href="http://gogle.com" target="_blank">Link<a><br>
    <a href="http://gmail.com" target="_blank">Mail<a>
  </div>
</section>

<ul class="tabs" data-tabgroup="second-tab-group">
  <li><a href="#group2-tab1" class="active">Tab 1</a></li>
  <li><a href="#group2-tab2">Tab 2</a></li>
  <li><a href="#group2-tab3">Tab 3</a></li>
</ul>
<section id="second-tab-group" class="tabgroup">
  <div id="group2-tab1">
    <h2>Heading 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
  </div>
  <div id="group2-tab2">
    <h2>Heading 2</h2>
    <p>Adipisci autem obcaecati velit natus quos beatae explicabo.</p>
  </div>
  <div id="group2-tab3">
    <h2>Heading 3</h2>
    <p>Atque ratione soluta laboriosam illo inventore amet?</p>
  </div>
</section>

$('.tabgroup > div').hide();
$('.tabgroup > div:first-of-type').show();
$('.tabs a').mouseover(function(e){
  e.preventDefault();
    var $this = $(this),
        tabgroup = '#'+$this.parents('.tabs').data('tabgroup'),
        others = $this.closest('li').siblings().children('a'),
        target = $this.attr('href');
    others.removeClass('active');
    $this.addClass('active');
    $(tabgroup).children('div').hide();
    $(target).show();

})

1 个答案:

答案 0 :(得分:1)

你有两个没有斜线的关闭锚标签。

</a>

Demo

请注意,为简单起见,我将hide()show()语句合并,并删除了不必要的e.preventDefault()语句。