在第二个 forEach() 中使用第一个 $(this)
Use first $(this) in second forEach()
我一直在网上寻找我面临的这个问题。
我找不到在我的第二个嵌套循环中使用我的第一个 $(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");
}
});
});
});
});
内部函数能够访问外部变量,因为内部函数对外部函数的变量具有闭包。
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- Javascript:表单验证getElementById仅返回第一个id元素
- 仅将 CSS 规则应用于
中的第一个 ,而不分配类/ID - JS在隐藏未定义的值后仅从数组中返回第一个id的值
- 高图表,检测点是否在堆栈中的第一个
- json结果显示第一个值
- 为什么当调用一个函数时,第一个this是父对象,但在随后的函数调用中,this是指窗口对象