[英]how to remove style added with css las-child
im facing problem to remove the style added with last-child
attribute of css 我面临的问题是删除添加有css的
last-child
属性的样式
for the first time i want last-list
item to be highlighted , when a particular element is clicked it has to be highlighted 我第一次希望突出显示
last-list
项,当单击特定元素时,必须突出显示它
$(function(){ $('.all-list li').click(function(){ $(this).css('background','red'); // tried to remove code added with csss, :last-child $('.all-list').last().css('background','none'); }); });
ul.all-list{ list-style:none; } ul.all-list li{ display:inline-block; padding:5px; font-size:20px; } ul.all-list li:last-child{ background:red; } active-class{ background:red !important; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="all-list"> <li>first</li> <li>second</li> <li>third</li> <li>fourth</li> </ul>
You can use sibling to remove color expect clicked one you can try this method 您可以使用同级色去除颜色,希望您单击后可以尝试此方法
$(function(){
$('.all-list li').click(function(){
$(this).css('background','red');
$(this).siblings().css('background', 'none');
});
});
Or 要么
$(function(){
$('.all-list li').click(function(){
$(this).css('background','red').siblings().css('background', 'none').end();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.