在第二个 forEach() 中使用第一个 $(this)

Use first $(this) in second forEach()

本文关键字:第一个 this 第二个 forEach      更新时间:2023-09-26

我一直在网上寻找我面临的这个问题。

找不到在我的第二个嵌套循环中使用我的第一个 $(this) 的方法。我想要像 Php 这样用别名做的事?

有人知道这是否可能吗?

您可以在下面看到我的代码来表示问题。

$(".btn-labeled").click(function()
{
  var uid = $(this).parent().prev("td").html();
  $.get( "?what="+uid, function( data ) {
  var parsedJson =  JSON.parse(data);
  $(".rule").each(function()
  {
    var serviceAccount = $(this).children("td").html();
    parsedJson.forEach(function(iteration)
    {
      if(iteration["service_account"] != serviceAccount)
      {
        //I want this to be attached to first loop :p
        $(this).children("td").next("td").children("div").children("input").removeAttr("checked");
      }
    });
  });

    });
});

提前致谢

创建一个变量element引用,该变量在内部循环中:

$(".btn-labeled").click(function()
{
  var uid = $(this).parent().prev("td").html();
  $.get( "?what="+uid, function( data ) {
  var parsedJson =  JSON.parse(data);
  $(".rule").each(function()
  {
   var element = $(this);//here
    var serviceAccount = $(this).children("td").html();
    parsedJson.forEach(function(iteration)
    {
      if(iteration["service_account"] != serviceAccount)
      {
       element.children("td").next("td").children("div").children("input").removeAttr("checked");
      }
    });
  });

    });
});

您可以将外部上下文对象设置为变量,然后在内部使用它:

 $(".rule").each(function(){
var obj= $(this);
var serviceAccount = $(this).children("td").html();
parsedJson.forEach(function(iteration)
{
  if(iteration["service_account"] != serviceAccount)
  {
    //I want this to be attached to first loop :p
    obj.children("td").next("td").children("div").children("input").removeAttr("checked");
  }
 });
});

只需将$(this)分配给一个变量,然后使用该变量即可。

喜欢var $this = $(this);

$(".btn-labeled").click(function()
{
  var uid = $(this).parent().prev("td").html();     
  $.get( "?what="+uid, function( data ) {
  var parsedJson =  JSON.parse(data);
  $(".rule").each(function()
  {
    var $this = $(this); // assign $(this) to a new variable
    var serviceAccount = $(this).children("td").html();
    parsedJson.forEach(function(iteration)
    {
      if(iteration["service_account"] != serviceAccount)
      {
        //Use that variable
        $this.children("td").next("td").children("div").children("input").removeAttr("checked");
      }
    });
  });

    });
});

Javascript 中this的作用域是动态作用域。这取决于函数的调用方式,而不是作者如何编写代码(如在词法范围内)

所以你的代码应该是这样的

$(".btn-labeled").click(function()
{
  var uid = $(this).parent().prev("td").html();
  $.get( "?what="+uid, function( data ) {
  var parsedJson =  JSON.parse(data);
  // save reference to this also it is good idea to prefix variables containing jQuery objects with a $
  var $outer_this = $(this) 
  $(".rule").each(function()
  {
    var serviceAccount = $outer_this.children("td").html();
    parsedJson.forEach(function(iteration)
    {
      if(iteration["service_account"] != serviceAccount)
      {
        //I want this to be attached to first loop :p
        $outer_this.children("td").next("td").children("div").children("input").removeAttr("checked");
      }
    });
  });

    });
});
内部函数

能够访问外部变量,因为内部函数对外部函数的变量具有闭包。