Can't update data-attribute value

后端 未结 9 1565
北海茫月
北海茫月 2020-11-27 17:11

Although there are some examples about this on the web, it does not seem to work correctly. I can\'t figure out the problem.

I have this simple html

         


        
9条回答
  •  感动是毒
    2020-11-27 17:19

    THE ANSWER BELOW IS THE GOOD ONE

    You aren't using the data method correctly. The correct code to update data is:

    $('#foo').data('num', num); 
    

    So your example would be:

    var num = $('#foo').data("num") + 1;       
    console.log(num)       
    $('#foo').data('num', num); 
    console.log(num)
    

提交回复
热议问题