When would I use JQuery.Callbacks?

后端 未结 6 1495
忘掉有多难
忘掉有多难 2021-01-30 01:34

I was looking through new stuff added to jQuery 1.7 and I saw they now have jQuery.Callbacks() http://api.jquery.com/jQuery.Callbacks/.

The documentation shows you how

6条回答
  •  梦如初夏
    2021-01-30 02:16

    I can see callbacks being useful when you are updating different DOM elements using the same method(s).

    Here is a cheesy example: http://jsfiddle.net/UX5Ln/

    var clickCallbacks = $.Callbacks();
    
    clickCallbacks.add(function() {
        var c = parseInt(this.text(), 10);
        this.text(c + 1);
    });
    clickCallbacks.add(function(id) {
        $('span', '#last').text(id);
    });
    
    $('.click').click(function() {
        var $ele = $(this).next('div').find('[id^="clickCount"]');
        clickCallbacks.fireWith($ele, [this.id]);
    });
    

    It updates the click counter and the 'last clicked' when you click on something.

提交回复
热议问题