在#post-index的div里,有若干个a,想要给被点击的a加上名为current的class,同时删除其他a上名为current的class。
下面是我自己写的,可用,但是很冗长。
请问这样的循环代码如何精简/合并?
$("#post-index a:nth-child(1)").click(function() {
$('*').removeClass('current')
$('#post-index a:nth-child(1)').addClass('current')
} );
$("#post-index a:nth-child(3)").click(function() {
$('*').removeClass('current')
$('#post-index a:nth-child(3)').addClass('current')
} );
$("#post-index a:nth-child(5)").click(function() {
$('*').removeClass('current')
$('#post-index a:nth-child(5)').addClass('current')
} );
$("#post-index a:nth-child(7)").click(function() {
$('*').removeClass('current')
$('#post-index a:nth-child(7)').addClass('current')
} );