2014-10-08 102 views
-2

我想獲得rel屬性,當我點擊一個按鈕如何:(與類選擇目標)獲得一個屬性當我點擊一個按鈕

<button class="nameClass" rel="relName">Content</button> 

對於我試圖

$(".nameClass").click(function(){ 
    // Here I want to get the rel content of the button that I clicked 
}) 

預先感謝 問候

+4

'$(本).attr( '相對');' – melancia 2014-10-08 08:39:07

+0

查找'jQuery'教程,並試圖用它來工作之前,給他們一展身手。學習'Javascript'也是必不可少的。 – melancia 2014-10-08 08:39:39

回答

2
$(".nameClass").click(function(){ 
    alert($(this).attr('rel')); 
}) 
0
$(".nameClass").click(function(){ 
// Here I want to get the rel content of the button that I clicked 
var myRel = $(this).attr('rel'); 
}) 
0

FIDDLE Attr用於通過提供鍵來獲取值,所以你使用rel就像屬性「name」一樣。

在這裏,你在這裏提供關鍵的rel,你會得到它的價值。

$(".nameClass").on("click",function(){ 
    console.log($(this).attr('rel')); 
})