Change CSS Link Property onClick with Javascript/JQuery?

后端 未结 7 2136
长情又很酷
长情又很酷 2021-01-01 04:34

So for example i have two links:

Link 1
Link 2
<
7条回答
  •  长情又很酷
    2021-01-01 05:13

    HTML

    Link 1
    Link 2
    

    Script (using jQuery)

    $(document).ready(function(){
        $('a').click(function(){
            $('a').css('color', 'black');
            $(this).css('color', 'blue');
        });
    });
    

    CSS

    a:link { color: black; }
    a:visited { color: black; }
    

    Fiddle here

    Note: The color change will be applied to all anchors current on your page. If you want to limit it to a select few, then put them in a class and add that class to the selector.

    Edit:

    If you plan on doing anything other than simple color swap, then classes are definitely the way to go (just substitute the .css calls for .addClass and .removeClass with your custom class names.

提交回复
热议问题