mhatch mhatch - 5 months ago 12
jQuery Question

jQuery tabs plugin creates extra anchor tags when nesting hyperlink

I am new to JavaScript and jQuery. While trying to nest anchor tags in the content div of a tabs plugin, extra empty actor tags are being created. How can I adapt the code to prevent this from happening. I have created a JSFiddle here: http://jsfiddle.net/p94aybey/

Thanks

<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();

})

Answer

You have two closing anchor tags lacking slashes.

</a>

Demo

Note that I've combined your hide() and show() statements for simplicity, and I've removed the unnecessary e.preventDefault() statement.