Javascript jQuery获取链接id

Javascript jQuery获取链接id,javascript,jquery,html-table,Javascript,Jquery,Html Table,我正在尝试获取动态生成表的id属性。因此,如果我点击第一个链接,我想得到“editEmploee-4” 您知道如何获取动态生成的链接id吗?事件委派: $(document).on("click", "#example a", function() { console.log(this.id); //id of clicked link }); 不确定动态生成了多少表,但您通常希望使用容器元素,而不是文档您可以在锚点的单击处理程序中使用此.id: $('.editDialog').cl

我正在尝试获取动态生成表的id属性。因此,如果我点击第一个链接,我想得到“editEmploee-4”

您知道如何获取动态生成的链接id吗?

事件委派:

$(document).on("click", "#example a", function() {
    console.log(this.id); //id of clicked link
});

不确定动态生成了多少表,但您通常希望使用容器元素,而不是
文档
您可以在锚点的单击处理程序中使用
此.id

$('.editDialog').click(function() {
    var data = this.id;
});

但是,由于您的表是动态生成的,因此此表及其内部的元素将无法使用所有事件,在这种情况下,您需要应用事件委派技术,以便将这些事件(如案例中的单击)附加到这些新添加的元素:

$(document.body).on("click", "#example a", function() {
    var data = this.id;
});
实际上,当您将委托事件绑定到最近的静态父级而不是
$(document)

时,效率会更高,因为在所有标记中都有一个类“editDialog”。我们可以在JQuery或Java脚本代码中使用:

JQuery::

$('.editDialog').bind('click', function(){
alert($(this).attr('id'));
});
Java脚本::

$('.editDialog').bind('click',function(){
var clickedID = this.id;
alert(clickedID);
});

你什么时候做这个?在
onclick
处理程序中?为什么使用jQuery?为什么不
this.id
?@karlandréGagnon习惯的力量我猜:)这些都是坏习惯:)@tymeJV肯定会的。我祈祷成为动态代码,消除ID和类上的拼写错误:)你认为它将如何工作?处理程序在运行时绑定,如果在处理程序不绑定后添加此内容?
$(".editDialog").click(function(e)
{
  e.preventDefault();
  var myID = $(this).attr("id");
 // do something with myID
})
$('.editDialog').bind('click', function(){
alert($(this).attr('id'));
});
$('.editDialog').bind('click',function(){
var clickedID = this.id;
alert(clickedID);
});