Difference between jQuery.one() and jQuery.on()

前端 未结 3 695
滥情空心
滥情空心 2020-12-20 12:18

I have multiple images on my page. To detect broken images , I used this found on SO.

$(\'.imgRot\').one(\'error\',function(){
    $(this).attr(\'src\',\'bro         


        
3条回答
  •  北海茫月
    2020-12-20 13:00

    Community wiki: This generic answer does not contribute to the question OP posted but relative to the title.

    The concept of one() and on() can be explained with the below code.

    one() function is automatically moved to off state after first instance of occurance.

    on() is identical to one() but it needs to be manually put to off state otherwise the number of instances has no limit.

    var i = 1;
    $('.one').one('click', function() {
    
      $(this).text('I am clickable only once: ' + i);
      i++;
    });
    
    var j = 1;
    $('.multiple').on('click', function() {
    
      $(this).text('I was clicked ' + j + ' times');
      j++;
    });
    
    
    Click me
    Click me

提交回复
热议问题