2014-02-26 110 views
0

我已经PHP脚本生成这些锚:jQuery的点击锚,并得到价值

<a href="#" class="remFile"> Leaf </a> 
<a href="#" class="remFile"> Flower </a> 
<a href="#" class="remFile"> Branches </a> 
<a href="#" class="remFile"> Seeds </a> 

我需要点击例如花和警觉“花”;

我用的jQuery:

alert($(".remFile").html()); // output 1st item "Leaf" on any anchor clicked 

我也试过:

alert($(".remFile").text()); // output all values on any anchor clicked 

如果是不可能的,可以请你提出一些其他的解决方案?喜欢用< li>?

+0

您收集了一些*不接受的*答案......真的是可能的,没有提供答案的帮助你的? –

回答

2

您可以使用$(this)瞄准当前点击的锚以及text()让你的锚文本:

$('a.remFile').click(function(e) { 
    e.preventDefault(); // Prevent default action of your anchor which will reload the page 
    alert($(this).text()); 
}); 

Fiddle Demo

1

只是用它来指代当前锚

alert($(this).text(); 

$('a').click(function(){ 
alert($(this).text()); 
}); 

Fiddle

1

尝试

$(".remFile").click(function(){ alert($(this).html())}); 
1

使用本 -

$('.remFile').click(function(e) { 
    e.preventDefault(); // or return false; 
    alert($(this).html()); 
}); 

现场演示:click here

0

正如我能理解你想这样做:

$(".remFile").click(function(){ 
    alert($(this).html()); 
});