문제

When I click the 'Learn More' button I would like to make the 'Third Link' button show as selected. Any solutions for this?

html

<ul>
    <li><a href="#">First Link</a></li>
    <li><a href="#">Second Link</a></li>
    <li><a href="#">Third Link</a></li>
    <li><a href="#">Fourth Link</a></li>
    <li><a href="#">Fifth Link</a></li>
</ul>
<ul>
    <li><a class="learnmore" href="#">Learn More</a></li>
</ul>

jquery

$('a').click( function() {
    $('a').not(this).removeClass('selected');
    $(this).addClass('selected');
});

Fiddle

도움이 되었습니까?

해결책

Add a class to your third link so that you can help jQuery identify it.

<li><a href="#" class="third">Third Link</a></li>

Then instruct your code to add the .selected class to it if an anchor with the .learnmore class is clicked.

$('a').click( function() {
$('a').not(this).removeClass('selected');
$(this).addClass('selected');
// new code below
if ($(this).hasClass('learnmore')) {
    $('a.third').addClass('selected');
};

Fiddle here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top