Javascript 需要获取当前元素的id

Javascript 需要获取当前元素的id,javascript,jquery,Javascript,Jquery,因此,我有几个基于类使用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(".nodeT

因此,我有几个基于类使用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")
}))

jQuery(“.nodeTable”).html(nodeHealthTable({
角色:jQuery(this.attr(“id”)
}))

这将用空字符串填充角色。如何访问当前元素的id?

在您的代码中不引用所选元素,
html
方法接受一个函数,在该函数的上下文中
引用当前元素(jQuery在内部使用
每个
方法)


this
在您的代码中不引用所选元素,
html
方法接受一个函数,在该函数的上下文中
this
引用当前元素(jQuery在内部使用
每个
方法)


@undefined不知道.html()也接受了一个函数,我将删除我的答案,因为答案中的.each()过于复杂。今天学到了一些新东西:D谢谢你@undefined不知道.html()也接受了一个函数,我将删除我的答案,因为答案中的.each()过于复杂。今天学到了一些新东西:D谢谢你!
jQuery(".nodeTable").html(function() {
    return nodeHealthTable({ Role: this.id });
})