开发者

Increasing a number by 1 using jquery?

开发者 https://www.devze.com 2023-02-09 08:11 出处:网络
$(\'.fav\').live(\'click\', function(e){ $(this).toggleClass(\'highlight\'); //increase the number by 1
$('.fav').live('click', function(e){
    $(this).toggleClass('highlight');
    //increase the number by 1

html:

<li class="fav light_gray_serif">5</li>

how can i use jquery to i开发者_JAVA技巧ncrease the number between the li everytime its clicked? thanks


var num = parseInt($.trim($(this).html()));
$(this).html(++num)


You want to take a look at .html() or .text(). Here is an example:

$(this).text(function(i, t) {
    return Number(t) + 1;
});


HTML:

<span id="counter">0</span>

jQuery:

$('#counter').text(Number($('#counter').text())+1);

You can increase the counter when clicking an existing button like this:

$(document).on('click','#your-button', function(){
  $('#counter').text(Number($('#counter').text())+1);
});


Just use a plugin.

(function($) {
    $.extend($.fn, {
         "addOne": function() {
              var num = parseInt(this.text(), 10);
              this.text(++num);
         },
         "subtractOne": function() {
              var num = parseInt(this.text(), 10);
              this.text(--num);
         }
    });
}(jQuery))

Then call

$(".fav").live("click", function(e) {
     $(this).toggleClass("highlight").addOne();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消