简体   繁体   English

jQuery-事件绑定和缓存的元素

[英]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() : 如果要在多个缓存的元素上绑定一个函数,则可以使用.add()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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