2011-09-22 36 views
0

我是新的jQuery和寫了一個切換功能,如下所示:jQuery函數,隱藏其功能符合特定模式的所有標籤

  $('.one-toggle').click(function(){ 
      $('.one-graph').toggle(); 
     }); 

基本上我想改變我的兩個CSS選擇器,使它們匹配任何具有一個切換和一個圖形的標籤。我不希望選擇器需要直接匹配。正則表達式可能?

+1

我相信你問什麼就解決了與「屬性包含」選擇器:http://api.jquery.com/category/selectors/ – Pat

+0

你能更詳細地描述你想要的行爲嗎?例如,顯示一些您希望通過選擇器匹配的不同元素? – jmar777

+0

我的意思是我也希望它匹配class =「one-toggle132414-river」我本質上尋找模糊匹配 – Spencer

回答

0

要匹配同時具有類名稱的標籤,你只需要這樣:

// cache the selector 
var $elements = $('.one-toggle.one-graph').click(function(){ 
    // toggle all matched elements 
    $elements.toggle(); 
    // or did you want to toggle just the clicked element? 
    // $(this).toggle(); 
}); 
+0

我正在尋找一個模糊的匹配不是一個或匹配。我的意思是我也希望它匹配class =「one-toggle132414-river」 – Spencer

1

只需使用標準的CSS選擇器分離器:

$('.one-toggle, .one-graph').click(function(){ 
    $('.one-graph').toggle(); 
});