2013-01-15 40 views
0

我試圖在我的網站上構建collase展開功能。使用jQuery切換.html文本

我有以下切換隱藏的div只是試圖改變'展開'文本崩潰,然後如果再次點擊,崩潰>展開。

可以anybvody看到我做錯了什麼?

$('.coverage .expand').click(function(){ 
    $(this).parent().parent().find('.subitems').toggle('slow', function() { 
     var togtit = $(this).parent().find('.expand'); 
     if((togtit).is('Expand')){ 
      togtit.html('Collapse') 
     } else { 
      togtit.html('Expand') 
      } 
    }); 
}); 

回答

1

問題是您以錯誤的方式使用.is()方法。您可以嘗試,而不是執行以下操作:

$(this).parent().find(".expand").html(function(i, html) { 
    return html === "Expand" ? "Collapse" : "Expand"; 
}); 
1
if(togtit.html() == 'Expand'){ 
    togtit.html('Collapse') 
} else { 
    togtit.html('Expand') 
} 

以上應該工作。

檢查當前匹配組針對一個選擇器,元件, 或jQuery對象元素,並且如果這些元素 給定的參數匹配中的至少一個返回true。

jQuery.is應該用來檢查元素是否匹配選擇器。它不會測試元素的內部內容。

+0

Thanks @FAngel ... – Liam