How to remove CSS class from one binding and assign it to another using jquery?

Here are my bindings

<a ID="lnkbtn0" class="page-numbers" href="#">1</a>
<a ID="lnkbtn1" class="page-numbers" href="#">2</a>
<a ID="lnkbtn2" class="page-numbers" href="#">3</a>
<a ID="lnkbtn3" class="page-numbers" href="#">4</a>

And my click function I add cssclass to show that this is the current anchor

$("a.page-numbers").click(function() {
                $(this).addClass('page-numbers current');
    });

What happens when I click the next anchor, the same class is added to the current and previous binding ... What can be done to remove cssclass page-numbers currentand assign cssclass to the page-numbersprevious / all other anchors without changing the current ....

+3
source share
7 answers

To avoid costly running through the entire house, do the following:

$("a.page-numbers").click(function() {
    $(this).addClass('current').siblings().removeClass('current');
});
+6
source

To follow @Danrumb answer :

current () a.page-numbers, :

$('a.page-numbers').click(function() {
    $('a.page-numbers').removeClass('current');
    $(this).addClass('current');
});
+1
$("a.page-numbers").click(function() {
    $("a.page-numbers.current").removeClass("current");
    $(this).addClass('current');
});
+1
$("a.page-numbers").click(function() {
                $(this).addClass('current').siblings('a').removeClass('current');
    });

, .

+1

"class" page-numbers class 2 : page-numbers current.

,

$(this).addClass('page-numbers');

current, :

$(this).removeClass('current');

. .

0

, - :

   $("a.page-numbers").click(function() {
                    $(this).addClass('current').prev('a').removeClass('current');
        });

, .

, :

1) 2) .

, ,

0
<a ID="lnkbtn0" class="page-numbers" href="#">1</a>
<a ID="lnkbtn1" class="page-numbers" href="#">2</a>
<a ID="lnkbtn2" class="page-numbers" href="#">3</a>
<a ID="lnkbtn3" class="page-numbers" href="#">4</a>

$("a.page-numbers").click(function() {
    $('a.page-numbers.current').removeClass('current');
    $(this).addClass('current');
});

page-numbers, .

0

Source: https://habr.com/ru/post/1739142/


All Articles