Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何访问不同级别的;这";在jquery中?_Javascript_Jquery_Oop - Fatal编程技术网

Javascript 我如何访问不同级别的;这";在jquery中?

Javascript 我如何访问不同级别的;这";在jquery中?,javascript,jquery,oop,Javascript,Jquery,Oop,我意识到下面的代码并不是获取元素的最有效的方法,但是为了举例说明 $('.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 occur

我意识到下面的代码并不是获取元素的最有效的方法,但是为了举例说明

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

将该变量存储在内部变量之前的变量中

$('.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?
   });
});
像这样的,

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

不需要存储变量。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
  });
});

这应该行得通。

var firstClassThis=this是多余的。jQuery已经管理了这些标识符。看看我的答案。如果你已经在使用索引,你的代码看起来会更好。如果我不关心索引,我可能会在局部变量中捕获“this”。True+1您应该为'me'变量指定'var'关键字。是的,我个人不喜欢任何地方的参数。使用var-imo更干净。
$('.myFirstClass').each(function(i){
   var me = this;
   $(this).find('.mySecondClass').each(function(j){
      alert($(me).attr('id'));
   });
});