@@ -493,11 +493,29 @@ $.Widget.prototype = {
493493 classes : this . options . classes || { }
494494 } , options ) ;
495495
496+ function bindRemoveEvent ( ) {
497+ options . element . each ( function ( _ , element ) {
498+ var isTracked = $ . map ( that . classesElementLookup , function ( elements ) {
499+ return elements ;
500+ } )
501+ . some ( function ( elements ) {
502+ return elements . is ( element ) ;
503+ } ) ;
504+
505+ if ( ! isTracked ) {
506+ that . _on ( $ ( element ) , {
507+ remove : "_untrackClassesElement"
508+ } ) ;
509+ }
510+ } ) ;
511+ }
512+
496513 function processClassString ( classes , checkOption ) {
497514 var current , i ;
498515 for ( i = 0 ; i < classes . length ; i ++ ) {
499516 current = that . classesElementLookup [ classes [ i ] ] || $ ( ) ;
500517 if ( options . add ) {
518+ bindRemoveEvent ( ) ;
501519 current = $ ( $ . unique ( current . get ( ) . concat ( options . element . get ( ) ) ) ) ;
502520 } else {
503521 current = $ ( current . not ( options . element ) . get ( ) ) ;
@@ -510,10 +528,6 @@ $.Widget.prototype = {
510528 }
511529 }
512530
513- this . _on ( options . element , {
514- "remove" : "_untrackClassesElement"
515- } ) ;
516-
517531 if ( options . keys ) {
518532 processClassString ( options . keys . match ( / \S + / g ) || [ ] , true ) ;
519533 }
@@ -531,6 +545,8 @@ $.Widget.prototype = {
531545 that . classesElementLookup [ key ] = $ ( value . not ( event . target ) . get ( ) ) ;
532546 }
533547 } ) ;
548+
549+ this . _off ( $ ( event . target ) ) ;
534550 } ,
535551
536552 _removeClass : function ( element , keys , extra ) {
@@ -611,7 +627,7 @@ $.Widget.prototype = {
611627 _off : function ( element , eventName ) {
612628 eventName = ( eventName || "" ) . split ( " " ) . join ( this . eventNamespace + " " ) +
613629 this . eventNamespace ;
614- element . off ( eventName ) . off ( eventName ) ;
630+ element . off ( eventName ) ;
615631
616632 // Clear the stack to avoid memory leaks (#10056)
617633 this . bindings = $ ( this . bindings . not ( element ) . get ( ) ) ;
0 commit comments