0

我希望 jQuery 函数知道单击了哪个链接来调用它,即我希望将链接的 id 值传递给 jQuery 函数。

这可能吗?如果是这样,那么最好的方法是什么。

4

4 回答 4

4

当然。在click()事件处理程序中,您可以引用单击的元素this

$("a").click(function() {
  alert(this.id);
  ...
});

或者

$("a").click(function() {
  alert($(this).attr("id"));
  ...
});
于 2009-12-08T07:35:27.523 回答
2
$("a").click(function() { 
   var linkid = $(this).attr("id");

   // use linkId here
});
于 2009-12-08T07:35:37.457 回答
1

不要忘记取消默认行为,否则您将一事无成。

$("a").click(function(e) {
   e.preventDefault();
   var linkid = $(this).attr("id");
   //do whatever here
});
于 2009-12-08T07:45:00.917 回答
0
$("a").click(function() {
  alert($(this).attr("id"));
  ...
});

我可以这么说..

于 2015-04-10T05:03:49.020 回答