How can I highlight a selected list item with jquery?

前端 未结 4 1489
长情又很酷
长情又很酷 2020-12-11 04:23

I have several items in a list and want to highlight the one a user clicks on by applying some css style, maybe a background color etc.

My HTML looks like this:

4条回答
  •  有刺的猬
    2020-12-11 04:58

    If you don't need the active to be persistent here's a CSS way:

    li:focus{
      background: red;
    }
    li:active{
      background: gold;
    }
    • Item 1
    • Item 2
    • Item 3
    Now click here and see why it's not persistent.

    in some situations the above might be useful - to only highlight the currently "click-active" item…

提交回复
热议问题