简体   繁体   中英

JQuery - Events binding and cached elements

Having some code like this one

$a = $('#a');
$b = $('#b');
$c = $('#c');

$('#a, #b, #c').keyup(function () {
    //Event handling
});

I would like to know

  1. If it is possible to use existing cached elements to handle events instead of the selector.
  2. If this is possible and proper, does it make any difference?

Of course, using cached elements is better for performance. If you want to bind a function on multiple cached elements, you can use .add() :

$a.add($b).add($c).keyup(function(){});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM