Javascript 动态生成id时获取链接的id

Javascript 动态生成id时获取链接的id,javascript,jquery,html,Javascript,Jquery,Html,我有一个div里面有很多东西。我在这个div中也有一个链接,它有一个动态生成的id。没有办法知道id是什么 单击该链接时,如何将该链接的id获取到变量jQuery中?在*.js文件中?试试这个 带有标记名 $('a').click(function(){ //alert($(this).attr('id'); alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper

我有一个div里面有很多东西。我在这个div中也有一个链接,它有一个动态生成的id。没有办法知道id是什么

单击该链接时,如何将该链接的id获取到变量jQuery中?在*.js文件中?

试试这个

带有标记名

$('a').click(function(){
 //alert($(this).attr('id');  
 alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
与班级一起

$('.classOfAnchor').click(function(){
 alert($(this).attr('id');
 alert(this.id);  //Better then the statement above as it is javascript with jQuery wrapper
});
试试这个

带有标记名

$('a').click(function(){
 //alert($(this).attr('id');  
 alert(this.id); //Better then the statement above as it is javascript with jQuery wrapper
});
与班级一起

$('.classOfAnchor').click(function(){
 alert($(this).attr('id');
 alert(this.id);  //Better then the statement above as it is javascript with jQuery wrapper
});

除非有东西给元素一个id,否则就不会有id

但是,为了便于解释,让我们假设该链接上肯定有一个ID

如果使用jQuery,只需选择其容器div,然后向下遍历到链接:
var myID=$('div#theContainingDiv').children('a').attr('id')


或者,你可以按照别人说的去做——那也行。

除非有什么东西给元素一个id,否则就不会有id

但是,为了便于解释,让我们假设该链接上肯定有一个ID

如果使用jQuery,只需选择其容器div,然后向下遍历到链接:
var myID=$('div#theContainingDiv').children('a').attr('id')


或者,你可以按照别人说的去做——那也行。

如果他不知道链接的ID是什么,他应该如何使用ID选择器将事件处理程序绑定到它?如果他不知道链接的ID是什么,他应该如何使用ID选择器将事件处理程序绑定到它?您可以唯一地标识
并且它内部只有一个链接吗?您可以唯一地标识
并且它内部只有一个链接吗?或者只需
this.ID
并保存两个不必要的函数调用。或者
this.id
并保存两个不必要的函数调用。