Nick's approach是好的,但我通過delegate
功能可能使用一個單一的處理器爲整個表(「事件代理」),而不是在每個按鈕單獨處理,這樣的:
$('#theTable').delegate("button.shownextrow", "click", function() {
$(this).closest("tr").next("tr").show();
});
Live example
除了其他的東西,它可以讓你添加更多的行到表對(並刪除行對),而不用擔心掛鉤/取消掛鉤事件處理程序。看來,這確實需要button
元素和table
元素之間沒有什麼直接的層次吃點擊...
這裏是我未使用delegate
,只是歷史的目的 — 年長例如哇不delegate
簡化代碼:
$('#theTable').click(function(event) {
var button = $(event.target).closest("button.shownextrow");
if (button.length > 0) {
button.closest("tr").next("tr").show();
}
});
Live example
我怎樣才能顯示一個隱藏的對象,在我剛剛顯示的tr? – user342391 2010-09-27 15:45:34
@ user342391 - 我不會在裏面隱藏任何東西......但是你可以在上面的例子中的'.show()'之後添加一個'.find('whatever')。show()'來查找/顯示一些東西內。 – 2010-09-27 15:47:10