Increment a number by clicking onto a button

前端 未结 2 1700
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 01:04

I have the number $likes_number in a div block on my page. I would like to know how to increment it dynamically by clicking on a button?

My code

相关标签:
2条回答
  • 2021-01-01 01:17
    <script type="text/javascript" src="//code.jquery.com/jquery-1.6.2.js"></script>
    <div id="likes">
        <span class="figure"></span>
    </div>
    <button type="button" id="like">Like</button>
    <script type="text/javascript">
        var clicks = 0; $("#like").click(function(){ clicks++; $('.figure').html(clicks);});
    </script>
    

    Live example: https://jsfiddle.net/nTmu7/2/

    0 讨论(0)
  • 2021-01-01 01:27
    $('#like').click(function() {
        $('#likes span').text( parseInt($('#likes span').text()) + 1 );
    });
    

    You can see an example here: http://jsfiddle.net/nayish/GCvnH/

    0 讨论(0)
提交回复
热议问题