Как мне получить доступ к различным уровням "ЭТОГО" в jquery? - PullRequest
2 голосов
/ 30 июля 2009

Я понимаю, что приведенный ниже код не самый эффективный способ захвата элементов, но для примера ...

$('.myFirstClass').each(function(i){
   // Here is the first 'THIS' occurrence
   $(this).find('.mySecondClass').each(function(j){
      // Here is the second 'THIS' occurrence
      // How do i access the first occurrence from here?
   });
});

Ответы [ 4 ]

4 голосов
/ 30 июля 2009

Как то так,

$('.myFirstClass').each(function(i){
   var firstClassThis = this;
   $(this).find('.mySecondClass').each(function(j){
      // Here is the second 'THIS' occurrence
      // How do i access the first occurrence from here?
      //You can use firstClassThis here due to closure. 
   });
});
4 голосов
/ 30 июля 2009

Нет необходимости хранить переменные. jQuery уже делает это во втором параметре ...

$(".myFirstClass").each(function(i, j){
  // I am represented as this or j
  $(j).find(".mySecondClass").each(function(a, b){
    // I am represented as this or b
    // I can communicate with j
  });
});
3 голосов
/ 30 июля 2009

Храните это в переменной перед внутренним.

$('.myFirstClass').each(function(i){
   //store this
   var $that = $(this);
   $(this).find('.mySecondClass').each(function(j){
      //$that.something
      // How do i access the first occurrence from here?
   });
});
1 голос
/ 30 июля 2009
$('.myFirstClass').each(function(i){
   var me = this;
   $(this).find('.mySecondClass').each(function(j){
      alert($(me).attr('id'));
   });
});

Это должно сработать.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...