2010-03-10 67 views
1

爲什麼這兩個函數有點奇怪的行爲,當我點擊thead時,tbody展開然後崩潰。jQuery函數的奇怪行爲

在第二個函數可以做一些測試,添加警報給if,else語句,但它只進入if語句,所以它只會滑動。

我注意到的另一件事是,當tbody中只有一行時,它工作正常。爲什麼?

我也嘗試添加返回false,沒有任何功能,它不會被調用兩次或任何其他地方。

我將不勝感激,如果我能得到一些解決這個問題。

謝謝。

Function 1 
$(function() { 
    $("table.section thead").click(function() { 
    $(this).next("table.section tbody").slideToggle("slow"); 

    }); 
}); 

Function 2 
$(function() { 
    $("table.section thead").click(function() { 
     var body = $(this).next("table.section tbody"); 
     if (body.is(":visible")) 
     body.slideUp("slow"); 
     else 
     body.slideDown("slow"); 
    }); 
}); 

UPDATE 表

<table> 
<thead> 
<tr><td>heading></td></tr> 
</thead> 
<tbody> 
<tr><td>content1</td></tr> 
<tr><td>content2</td></tr> 
</tbody> 
</table> 
+0

這兩個函數是同時綁定還是這兩次嘗試做同樣的事情? – Sorpigal 2010-03-10 13:07:34

+0

你能告訴我們你的桌子嗎? – hallie 2010-03-10 13:08:29

+0

OP重複問題 http://stackoverflow.com/questions/2415999/jquery-help-converting-simple-function-please-help/2416034#2416034 – 2010-03-10 13:09:34

回答

0

我查看了jQuery郵件列表,這是高度依賴於瀏覽器的,因爲它們處理表以及它們如何完全不同。相反,我會嘗試在不同的元素頭部和滑動整個表,或相反,褪去了身上,這樣的事情:

$(function() { 
    $("table.section thead").click(function() { 
    $(this).next("tbody").animate({opacity: 'toggle'}); 
    }); 
}); 

動態調整個別表組件的尺寸,並讓它成爲跨正確的瀏覽器似乎不值得修復他的jQuery框架,所以我不希望這個改變很快。

0

目前還不清楚,但如果你在你的腳本有兩種,您呼叫的。點擊()這相當於兩次:

$(function() { 
    $("table.section thead").click(function() { 
     $(this).next("table.section tbody").slideToggle("slow"); 
     var body = $(this).next("table.section tbody"); 
     if (body.is(":visible")) 
     body.slideUp("slow"); 
     else 
     body.slideDown("slow"); 
    }); 
});