<ul>
<li><a href="#banner"><span class="list-nav"></span></a></li>
<li><a href="#section-one"><span class="list-nav"></span></a></li>
<li><a href="#section-two"><span class="list-nav"></span></a></li>
<li><a href="#section-three"><span class="list-nav"></span></a></li>
</ul>
$('.list-nav').first().addClass("spanList");
$('.list-nav').on('click',function(){
$(this).css("background","#6090b6");
$(this).addClass("spanList");
$(this).siblings().removeClass("spanList");
var href = $(this).attr("href");
var pos = $(href).offset().top;
$("html,body").animate({scrollTop: pos}, 1000);
return false;
})
I want to change the color of this when clicked, but other sibling li cancel the existing background color. And when other sibling elements are clicked, the sibling elements get the background color, and the background color of this is cancelled.
According to the idea of your code, you just want to operate the span element and have no intention of operating li, so the code is implemented like this
Online Demo
…$this.addClass("").closest("li").siblings("li").find("").removeAttr("")