Domanda

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 :(

È stato utile?

Soluzione

$('.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));
});

Altri suggerimenti

$('.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));
  });
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top