将 jQuery 侦听器附加到所有 <a> 元素并获取它们的 ID?

You*_*eat 0 jquery event-listener

我需要获取所有<a>链接的 ID,以便我可以根据它们的内容制作 Ajax 帖子

$(document).ready(function(e) {
   $('a').click(function(e) {
       var linker = this.attr('id');
       alert(linker);
       // Here I will then make an Ajax call, but I know how to do that :)
   });
});
Run Code Online (Sandbox Code Playgroud)

Ry-*_*Ry- 5

this不是一个 jQuery 对象,所以你不能调用attr它;您需要$(this)该方法可用。

$(document).ready(function(e) {
   $('a').click(function(e) {
       var linker = $(this).attr('id');
       alert(linker);
       // Here I will then make an Ajax call, but I know how to do that :)
   });
});
Run Code Online (Sandbox Code Playgroud)

或者,你可以这样做:

var linker = this.id;
Run Code Online (Sandbox Code Playgroud)

但这还不够 jQuery ;)