change background color of li using jquery

前端 未结 4 1333
情书的邮戳
情书的邮戳 2021-01-22 23:56

I want to change the color of li which contains anchor when the mouse go over it, I make like this

 
4条回答
  •  再見小時候
    2021-01-23 00:36

    Do it like this:

    $("a[id='son']").hover(function(){
       $(this).closest("li.sonItem").css("background-color","black");  
    });
    

    NOTE: This will work ok, however you should never have more than 1 element with the same id (you can, but it's a very, very bad practice), it'd be better if you use a class, for example class="son", and your selector would be $("a.son").

    Cheers

提交回复
热议问题