jquery .on itself

后端 未结 3 1187
闹比i
闹比i 2021-01-21 12:55

I want an element to listen for a custom event that is actually triggered by itself. The custom event could possibly be triggered from a descendant but should then be ignored. I

3条回答
  •  甜味超标
    2021-01-21 13:40

    .on works fine; to ignore ancestors check e.target:

    $el.on('customEvent', function(e) {
      if(e.target === this) {
        //Do whatever
      }
    });
    

提交回复
热议问题