如何使用jquery获得点击链接的href ?

时间:2022-12-27 21:22:32

Does anyone know how can I get the clicked link's href with jquery? I have the link as following:

有人知道我如何用jquery获得点击链接的href吗?我有以下链接:

    <a  href="ID=1" class="testClick">Test1.</a>
    <br />
    <a  href="ID=2" class="testClick">Test2.</a>
    <br />
    <a  href="ID=3" class="testClick">Test3.</a>

I wrote a code as following to get the href value from the link I clicked on. But somehow this is always return me the 1st link's href (ID=1) even though I clicked on Test2 or Test3. Does anyone know what is it going on here? and how can I solve this issue?

我编写了一个代码,以从我点击的链接获取href值。但无论如何,它总是返回给我第一个链接的href (ID=1),即使我单击了Test2或Test3。有人知道这是怎么回事吗?我该如何解决这个问题呢?

    $(".testClick").click(function () {
        var value = $(".testClick").attr("href");
        alert(value );
    });

4 个解决方案

#1


140  

this in your callback function refers to the clicked element.

这个在回调函数中引用了被单击的元素。

   $(".addressClick").click(function () {
        var addressValue = $(this).attr("href");
        alert(addressValue );
    });

#2


13  

You're looking for $(this).attr("href");

你正在寻找(美元).attr(“href”);

#3


9  

$(".testClick").click(function () {
         var value = $(this).attr("href");
         alert(value );     
}); 

When you use $(".className") you are getting the set of all elements that have that class. Then when you call attr it simply returns the value of the first item in the collection.

当您使用$(“. classname”)时,您将获得具有该类的所有元素的集合。然后,当您调用attr时,它只返回集合中第一个项的值。

#4


1  

Suppose we have three anchor tags like ,

假设我们有三个锚标签,

<a  href="ID=1" class="testClick">Test1.</a>
<br />
<a  href="ID=2" class="testClick">Test2.</a>
<br />
<a  href="ID=3" class="testClick">Test3.</a>

now in script

现在的脚本

$(".testClick").click(function () {
        var anchorValue= $(this).attr("href");
        alert(anchorValue);
});

use this keyword instead of className (testClick)

使用此关键字而不是className (testClick)

#1


140  

this in your callback function refers to the clicked element.

这个在回调函数中引用了被单击的元素。

   $(".addressClick").click(function () {
        var addressValue = $(this).attr("href");
        alert(addressValue );
    });

#2


13  

You're looking for $(this).attr("href");

你正在寻找(美元).attr(“href”);

#3


9  

$(".testClick").click(function () {
         var value = $(this).attr("href");
         alert(value );     
}); 

When you use $(".className") you are getting the set of all elements that have that class. Then when you call attr it simply returns the value of the first item in the collection.

当您使用$(“. classname”)时,您将获得具有该类的所有元素的集合。然后,当您调用attr时,它只返回集合中第一个项的值。

#4


1  

Suppose we have three anchor tags like ,

假设我们有三个锚标签,

<a  href="ID=1" class="testClick">Test1.</a>
<br />
<a  href="ID=2" class="testClick">Test2.</a>
<br />
<a  href="ID=3" class="testClick">Test3.</a>

now in script

现在的脚本

$(".testClick").click(function () {
        var anchorValue= $(this).attr("href");
        alert(anchorValue);
});

use this keyword instead of className (testClick)

使用此关键字而不是className (testClick)