2010-10-19 49 views
0

我想用表格行實現切換功能。除了IE8以外,一切都很順利。我使用的腳本如下所示。jquery無法使用IE 8

<script src="jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#report tr:odd").addClass("odd"); 
     $("#report tr:not(.odd)").hide(); 
     $("#report tr:first-child").show(); 

     $("#report tr.odd").click(function(){ 
      $(this).next("tr").each(function(){ this.toggle()}); 
      $(this).find(".arrow").toggleClass("up"); 
     }); 
     //$("#report").jExpand(); 
    }); 
</script>   

有人請幫助我..

傑西卡

+0

什麼不起作用?你會得到什麼錯誤? – 2010-10-19 10:40:06

+0

當我點擊行,切換功能(隱藏和顯示內容)不工作.. – jessi 2010-10-19 10:41:09

+0

我使它$(this).next(「tr」)。toggle();但不工作(只在IE 8中),有沒有解決方案?請幫幫我。 – jessi 2010-10-19 10:50:53

回答

1

你需要用($())您this(這僅僅是環內的DOM元素,而不是一個jQuery對象)來訪問到.toggle(),像這樣:

$(this).toggle() 

但沒有必要爲.each()環這裏,這樣的:

$(this).next("tr").each(function(){ $(this).toggle()}); 

可以僅僅是這樣的:

$(this).next("tr").toggle(); 

,它會在發現這些元素的所有操作......即使有將只有一個在這裏。


問題#2是IE8明確認爲下一行是總是都看得到(這是你使用的的jQuery 1.3.2實現中的錯誤)的事實。你有兩個選擇這裏,快速修復是將其重新寫這樣的:

$("#report tr.odd").click(function(){ 
    var show = $(this).find(".arrow").toggleClass("up").hasClass("up"); 
    $(this).next("tr").toggle(show); 
}); 

You can see that working here。或者給我一個更好的解決方案,升級到最新的jQuery(1.4.3),you can test your current code here against it,也在IE8中工作。

+0

後來就是這樣。但是太失敗了。 – jessi 2010-10-19 10:46:11

+0

@jessi - 您是否有示例頁面?我們不能說沒有標記就能100%解決你的問題,但是我描述/展示瞭如何解決上述問題的確存在100%的錯誤。 – 2010-10-19 10:50:30

+0

是的,我從下面的網址得到的例子http://www.jankoatwarpspeed.com/examples/expandable-rows/ – jessi 2010-10-19 10:51:44

0

在你行:

$(this).next("tr").each(function(){ this.toggle()}); 

嘗試將其更改爲:

$(this).next("tr").each(function(){ $(this).toggle()}); 

我有這個問題之前,在某些瀏覽器不能夠這樣和$(本)之間進行區分。

所以你會得到:

$(document).ready(function(){ 
    $("#report tr:odd").addClass("odd"); 
    $("#report tr:not(.odd)").hide(); 
    $("#report tr:first-child").show(); 

    $("#report tr.odd").click(function(){ 
    $(this).next("tr").each(function(){ $(this).toggle()}); 
    $(this).find(".arrow").toggleClass("up"); 
    }); 
}); 
+1

這不是一個瀏覽器的具體問題,這將永遠不會工作。 – 2010-10-19 10:42:30

+0

完成,但它不工作.. – jessi 2010-10-19 10:45:24

+0

哎呀,你是對的。我認爲當我遇到問題時可能會有一些不同。 – EMMERICH 2010-10-19 10:47:26