jquery highlight the link when clicked

后端 未结 7 1689
梦毁少年i
梦毁少年i 2021-01-14 22:41

How do I use jquery to highlight the link when I click on it?

For example, when I click on the link class1_1, I want to make this link red (or another color).

<
7条回答
  •  失恋的感觉
    2021-01-14 23:08

    This should work:

    Javascript:

    $(function(){
        $('.class1').click(function() {
            $(this).toggleClass('active1');
        });
    });
    

    CSS:

    a.class1 { color: red; }
    a.active1 { color: blue; }
    

    HTML:

    some text
    

    Its better to use toggleClass (2 in 1) instead of addClass/removeClass.

提交回复
热议问题