2014-02-26 103 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()); 
});