开发者

Caching objects in variables or in $(this).data

开发者 https://www.devze.com 2023-03-10 00:05 出处:网络
I was wondering what could be the better approach. This: var strips = $(\'.strip\'); for (var i = 0; i &l开发者_StackOverflowt; strips.length; i++) {

I was wondering what could be the better approach. This:

var strips = $('.strip');
for (var i = 0; i &l开发者_StackOverflowt; strips.length; i++) {
  (function() {
    var strip = $(strips[i]);
    var controls = $('.controls', strip);
    controls.click(function() {
      alert(strip.attr('id'));
    }
  })();
}

Or this:

$('.strips .controls').click(function() {
  var me = $(this);
  if (!me.data('strip')) { me.data('strip', me.parents('.strip')) }
  alert(me.data('strip').attr('id'));
}

Is there really a difference besides personal preference?

0

精彩评论

暂无评论...
验证码 换一张
取 消