所以,我有几个表,我基于类使用 jQuery 迭代,但我的行为需要根据我所在的元素的 id 稍微改变。这是我的代码:
<table id="Brokers" class="nodeTable" border=1 />
<table id="Controllers" class="nodeTable" border=1 />
<table id="Cluster-Drivers" class="nodeTable" border=1 />
jQuery(".nodeTable").html(nodeHealthTable({
Role: jQuery(this).attr("id")
}))
这最终会用空字符串填充 Role。如何访问当前元素的 id?
最佳答案
this
在您的代码中不引用所选元素,html
方法接受一个函数,在此函数 this
的上下文中引用当前元素(jQuery 在内部使用 each
方法)。
jQuery(".nodeTable").html(function() {
return nodeHealthTable({ Role: this.id });
})
关于javascript - 需要获取当前元素的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14782999/