jquery highlight the link when clicked

后端 未结 7 1690
梦毁少年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:09

    Javascript:

    $('.link').click(function() {
        if (!$(this).hasClass('hi')) {
            // If this link is not already highlighted, highlight it and make
            // sure other links of class .link are not highlighted.
            $('.hi').removeClass('hi');
            $(this).addClass('hi');
        }
    });
    

    css:

    a.hi { color: red; }
    

    html:

    my text
    that text
    this text
    

提交回复
热议问题