質問

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