Add and remove attribute with jquery

前端 未结 4 1142
北恋
北恋 2020-12-04 21:28

This is my buttons:



this is my JavaScript cod

4条回答
  •  悲哀的现实
    2020-12-04 21:55

    It's because you've removed the id which is how you're finding the element. This line of code is trying to add id="page_navigation1" to an element with the id named page_navigation1, but it doesn't exist (because you deleted the attribute):

    $("#page_navigation1").attr("id","page_navigation1");
    

    Demo:

    If you want to add and remove a class that makes your

    red use:

    $( '#page_navigation1' ).addClass( 'red-class' );
    

    And:

    $( '#page_navigation1' ).removeClass( 'red-class' );
    

    Where red-class is:

    .red-class {
        background-color: red;
    }
    

提交回复
热议问题