jQuery increment value of tag

后端 未结 7 821
小鲜肉
小鲜肉 2020-12-31 04:12

I have a number in a span tag. I need to get the value of the number in the span and then increment it inside jQuery. I know how to do this for a text input - can this be

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 04:29

    This is the most complete answer.

    1. This solution avoids selector repetition
    2. Handles the increment gracefully if the span does not have a value yet.
    
    
    var $number = $('.changeNumber');
    $number.html((parseInt($number.html(),10) || 0) + 1);
    

提交回复
热议问题