开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜