2012-05-06 44 views
1

我使用「活」的功能上做錶行一些上點擊的東西,即如何找出哪個錶行被使用jQuery點擊?

$("tr").live('click',function() { 
     alert('Some table row is clicked'); 
}); 

我想找出被點擊哪一行,並使用if-else,給基於這樣一些自定義的警報。誰能告訴我該怎麼做?

非常感謝。

編輯1:

有沒有通過可以參考我在函數內部的點擊行的元素的方法嗎?

+5

$(this).index(); – KBN

+2

@xFortyFourx:如果您發表評論爲答案,我會接受它。正是我想要的! – Bhushan

回答

6
$("tr").live('click', function() { 
    if (this.id == "foo") { 
     alert('the tr with the foo id was clicked'); 
    } 
});​ 

如果你想檢查哪些行號,使用index

$("tr").live('click', function() { 
    if $(this).index() === 2) { 
    alert('The third row was clicked'); // Yes the third as it's zero base index 
    } 
});​ 

Live demo


更新:

$("tr").live('click', function() { 
    // "this" is the clicked <tr> element 
    // $(this).find('td span') is the spans inside a td inside the clicked <tr> 
} 
+0

+1 !!!!!非常感謝! – Bhushan

+0

謝謝你的回答。我有另一個問題。有什麼方法可以引用函數內點擊行的元素? – Bhushan

+1

@Learner。我看到了更新,並已更新我的... – gdoron

0

您可以使用this訪問點擊元素。

offtopic:$ .live從1.7開始被棄用,您應該使用$ .on。有關更多信息,請參閱here

1

演示:http://jsfiddle.net/zJUuX/

HTML:

<table> 
    <tr><td>hey</td></tr> 
    <tr><td>hi</td></tr> 
</table> 

的Jquery:

$("table tr").click(function(){ 
    messages($(this).index()); 
}); 

    function messages(index) { 
     switch(index){ 
      case 0: 
       alert("you clicked 1st row"); 
       break; 
      case 1: 
       alert("you clicked 2nd row"); 
       break; 
      default: 
       break; 
     } 
     $("table tr").eq(index).css("background","#ff0"); 
     $("table tr").eq(index).find("a"); //will find all the nested anchor tags. 
    } 

你去那裏學習,現在我必須接受我的虛擬點:d。玩的開心。

+0

+ 1 !!!!!!!!!!!!!!!!!!!! – Bhushan

+0

感謝您的回答。我有另一個問題。有什麼方法可以引用函數內點擊行的元素? – Bhushan

+0

$(「table tr」)。eq(index); < - 這將在函數「messages()」中得到選定的行。 – KBN

-1
//Even row 
$("tr:even").click(function() { 
    alert('Even'); 
}); 

//Odd row 
$("tr:odd").click(function() { 
    alert('Odd'); 
});​ 
0

上gdoron的回答改善jQuery的生活()已過時,嘗試delegateon

$("#mytable").delegate("tr", "click", function(e) { 
    if (this == "foo") { 
    .... 
    } 
}); 
+0

如果您不能使用'.on()',則沒有理由在文檔上使用委託而不是'.live()'。它僅僅被棄用,而不贊成'.delegate()'(自從添加了'.on()'後也不贊成) – ThiefMaster

+0

@ThiefMaster。 '代表'沒有被棄用(但...) – gdoron

+0

好吧,它只是被取代。但是我會說,如果你有1.7,你可以把它當成實際上不推薦使用的。 – ThiefMaster

2

首先你不應該使用.live()曾經:)

why you shouldn't use .live()

你可以用.delegate()
E xample

$(document).delegate("tr", "click", function(e) { 
// write your code here 
}); 
+0

除非你使用jQuery <1.4.3 ... – gdoron

+0

沒錯,但即使你使用JQuery <1.4.3,如果你在你的代碼中使用它,特別是使用.live()會是一個不好的做法。 –

+1

感謝Rayan,+1的鏈接。 – Bhushan

2

讓我建議一個簡單的方法。假設這是你的表:

<table> 
<tr id = '1' class="tr">...</tr> 
<tr id = '2' class="tr">...</tr> 
<tr id = '3' class="tr">...</tr> 
</table> 

將此放在您的jQuery代碼:

$(function(){ 

$('.tr').click(function(){ 
var row_no = $(this).attr('id'); 
alert('Row number '+row_no+' was clicked'); 
}); 

}); 

希望這有助於你。

+1

你可以簡單地使用'this.id' – ThiefMaster

+0

謝謝!即使我是JQuery的新手 – Anwar

+0

這不是真正的jQuery - 「this」是普通的DOM元素,而「id」是每個DOM元素都有的屬性。所以在這種情況下,訪問它的直接方式更短更快。除此之外,如果您更願意使用jQuery方式訪問該屬性,請使用'prop'而不是'attr'。 'attr'有一些魔法來檢測你是否真的想要一個屬性而不是屬性,所以它比較慢。通常'.prop()'就是你想要的 - 在很少的情況下,你真的需要'.attr()' – ThiefMaster