javascript - How to get css button to stay active after it has been clicked? - Stack Overflow

admin2025-03-19  1

Im trying to make a nav bar where once the user clicks on the image, the image stays active. In the following example the leaf would stay green after is is clicked. Here is a bit of code of what Im talking about:

<a class="myButtonLink" href="#LinkURL">Leaf</a>

<style>
 .myButtonLink {
    display: block;
    width: 100px;
    height: 100px;
    background: url('.png') bottom;
    text-indent: -99999px;
 }
 .myButtonLink:hover {
    background-position: 0 0;
 }
</style>

Im trying to make a nav bar where once the user clicks on the image, the image stays active. In the following example the leaf would stay green after is is clicked. Here is a bit of code of what Im talking about:

<a class="myButtonLink" href="#LinkURL">Leaf</a>

<style>
 .myButtonLink {
    display: block;
    width: 100px;
    height: 100px;
    background: url('http://kyleschaeffer./wordpress/wp-content/uploads/2009/01/buttonleafhover.png') bottom;
    text-indent: -99999px;
 }
 .myButtonLink:hover {
    background-position: 0 0;
 }
</style>
Share Improve this question edited Jan 12, 2012 at 5:18 Dipu Raj 1,8944 gold badges33 silver badges39 bronze badges asked Jan 12, 2012 at 4:16 BrambleBramble 1,39513 gold badges39 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

http://jsfiddle/bnaegele/XHBZf/2/

$('.myButtonLink').click(function() {
     $(this).css('background-position', '0 0');
 });

You could apply a class to it on click.

$('.myButtonLink').click(function() {
    $(this).toggleClass('active');
});

This code has an added effect of deselecting the leaf on a second click. Depending on your requirements, you might want it or not.

Example: http://jsfiddle/stulentsev/XHBZf/1/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742375142a210398.html

最新回复(0)