@@ -493,11 +493,29 @@ $.Widget.prototype = {
493
493
classes : this . options . classes || { }
494
494
} , options ) ;
495
495
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
+
496
513
function processClassString ( classes , checkOption ) {
497
514
var current , i ;
498
515
for ( i = 0 ; i < classes . length ; i ++ ) {
499
516
current = that . classesElementLookup [ classes [ i ] ] || $ ( ) ;
500
517
if ( options . add ) {
518
+ bindRemoveEvent ( ) ;
501
519
current = $ ( $ . unique ( current . get ( ) . concat ( options . element . get ( ) ) ) ) ;
502
520
} else {
503
521
current = $ ( current . not ( options . element ) . get ( ) ) ;
@@ -510,10 +528,6 @@ $.Widget.prototype = {
510
528
}
511
529
}
512
530
513
- this . _on ( options . element , {
514
- "remove" : "_untrackClassesElement"
515
- } ) ;
516
-
517
531
if ( options . keys ) {
518
532
processClassString ( options . keys . match ( / \S + / g ) || [ ] , true ) ;
519
533
}
@@ -531,6 +545,8 @@ $.Widget.prototype = {
531
545
that . classesElementLookup [ key ] = $ ( value . not ( event . target ) . get ( ) ) ;
532
546
}
533
547
} ) ;
548
+
549
+ this . _off ( $ ( event . target ) ) ;
534
550
} ,
535
551
536
552
_removeClass : function ( element , keys , extra ) {
@@ -611,7 +627,7 @@ $.Widget.prototype = {
611
627
_off : function ( element , eventName ) {
612
628
eventName = ( eventName || "" ) . split ( " " ) . join ( this . eventNamespace + " " ) +
613
629
this . eventNamespace ;
614
- element . off ( eventName ) . off ( eventName ) ;
630
+ element . off ( eventName ) ;
615
631
616
632
// Clear the stack to avoid memory leaks (#10056)
617
633
this . bindings = $ ( this . bindings . not ( element ) . get ( ) ) ;
0 commit comments