Add and remove attribute with jquery

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

This is my buttons:



this is my JavaScript cod

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 21:44

    Once you remove the ID "page_navigation" that element no longer has an ID and so cannot be found when you attempt to access it a second time.

    The solution is to cache a reference to the element:

    $(document).ready(function(){
        // This reference remains available to the following functions
        // even when the ID is removed.
        var page_navigation = $("#page_navigation1");
    
        $("#add").click(function(){
            page_navigation.attr("id","page_navigation1");
        });     
    
        $("#remove").click(function(){
            page_navigation.removeAttr("id");
        });     
    });
    

提交回复
热议问题