문제

Hello people here is my code below..

$('.first').click(function(){

$('.second').each(function(){
console($(this));
});
});

i want to refer console($(this)); to $('.first') not the $('.second') .. i think we can do it through reference variable , but still not fixing :(

도움이 되었습니까?

해결책

$('.first').click(function(){
  var self = this;
  $('.second').each(function(){
    console($(self));
  });
});

or using jQuery.proxy() method:

$('.first').click(function(){
  $('.second').each($.proxy(function(){
    console($(this));
  }, this));
});

다른 팁

$('.first').click(function(){
  $('.second').each(function(){
    console($(this));
  }.bind(this));
});

Since this depends on context, you could make this clearer by being explicit about what you expect this to be:

$('.first').click(function(){
  var first = this;
  $('.second').each(function(){
    console($(first));
  });
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top