如何获得对单击的超链接的引用

Pos*_*Guy 1 jquery

我知道我可以在超链接元素上使用.click()方法.但是我怎么知道点击了哪个元素?首先,我必须参考超链接的ID.

所以假设我在视图源中有一个像这样的超链接页面:

<a href="addButton1" href="...someurl"><img src="somebutton"></a>
<a href="addButton2" href="...someurl"><img src="somebutton"></a>
<a href="addButton3" href="...someurl"><img src="somebutton"></a>
<a href="addButton4" href="...someurl"><img src="somebutton"></a>
Run Code Online (Sandbox Code Playgroud)

当用户点击addButton1时,我怎么知道它首先被点击的addButton1,以便我现在可以在它上面应用.click()事件?

Jon*_*anK 5

要观察页面上所有链接的点击事件:

$("a").click(function (e) { 
      // this function fires anytime a hyperlink is clicked

      e.preventDefault();

      // reference $(this) to get at the specific 
      // link that fired the event, such as:
      alert($(this).attr('href')); // display href value of the hyperlink
    });
Run Code Online (Sandbox Code Playgroud)

要观察页面上链接子集的单击事件:

在超链接标记中添加一个类.例:<a class="observe" href="#">

并将上述函数事件签名更改为:

$("a.observe").click(function (e) { ... });
Run Code Online (Sandbox Code Playgroud)