[英]Each link should open a div
When I click and select the first a in the mainPart
, I have to select the first subLink
div in the secondPart
div.. so other subLink
classes are not show.. And it should be in that order.. second a.link selected, second sublink
selected.... with jquery 当我单击并选择
mainPart
的第一个a时,我必须选择secondPart
div中的第一个subLink
div。这样其他subLink
类就不会显示。而且应该按该顺序进行。选择第二sublink
...。用jQuery
<div class="mainPart">
<a href="#" title="" class="link selected">first link</a>
<a href="#" title="" class="link">second link</a>
</div>
<div class="secondPart">
<div class="subLink selected">
<a href="#" title="" class="lmLink">....</a>
<a href="#" title="" class="lmLink">....</a>
</div>
<div class="subLink">
<a href="#" title="" class="lmLink">....</a>
<a href="#" title="" class="lmLink">....</a>
</div>
</div>
You could use index for that, by getting the clicked anchor index then select the div with the same index in the secondPart
: 您可以为此使用索引,方法是获得单击的锚索引,然后在
secondPart
选择具有相同索引的div:
$('.mainPart>a').on('click', function(){
var index = $(".mainPart>a").index($(this));
$('.secondPart .subLink').hide().eq(index).show();
})
Hope this helps. 希望这可以帮助。
$('.mainPart>a').on('click', function(){ var index = $(".mainPart>a").index($(this)); $('.secondPart .subLink').hide().eq(index).show(); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mainPart"> <a href="#" title="" class="link selected">first link</a> <a href="#" title="" class="link">second link</a> </div> <div class="secondPart"> <div class="subLink selected"> <a href="#" title="" class="lmLink">1.1...</a> <a href="#" title="" class="lmLink">1.2...</a> </div> <div class="subLink"> <a href="#" title="" class="lmLink">2.1...</a> <a href="#" title="" class="lmLink">2.2...</a> </div> </div>
Alternatively, you can achieve the same effect without javascript by making use of :target
pseudo selector in CSS, adding id
attribute to the .subLink
divs, like below: 或者,您可以通过使用CSS中的
:target
伪选择器,向.subLink
div中添加id
属性,而无需使用JavaScript来达到相同的效果,如下所示:
.subLink { display: none; } .subLink:target { display: inline-block; }
<div class="mainPart"> <a href="#sub1" title="" class="link selected">first link</a> <a href="#sub2" title="" class="link">second link</a> </div> <div class="secondPart"> <div id="sub1" class="subLink"> <a href="#" title="" class="lmLink">first 1</a> <a href="#" title="" class="lmLink">first 2</a> </div> <div id="sub2" class="subLink"> <a href="#" title="" class="lmLink">second 1</a> <a href="#" title="" class="lmLink">second 2</a> </div> </div>
Update: 更新:
For the hover
event with some javascript, using jQuery hover()
function: 对于使用某些JavaScript的
hover
事件,请使用jQuery hover()
函数:
$('.mainPart a').hover(function() { $('.subLink' + $(this).attr('href')).show(); }, function() { $('.subLink' + $(this).attr('href')).hide(); })
.subLink { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="mainPart"> <a href="#sub1" title="" class="link selected">first link</a> <a href="#sub2" title="" class="link">second link</a> </div> <div class="secondPart"> <div id="sub1" class="subLink"> <a href="#" title="" class="lmLink">first 1</a> <a href="#" title="" class="lmLink">first 2</a> </div> <div id="sub2" class="subLink"> <a href="#" title="" class="lmLink">second 1</a> <a href="#" title="" class="lmLink">second 2</a> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.