2012-05-12 11 views
2

mysiteにtoggle属性を追加したいと思います。 hide/showボタンをクリックすると、divを表示/非表示にする必要があります。アルファベット順によると、文字をクリックしたユーザーは、その文字で始まるコンテンツを表示します。だから私のコードはここにあります:jqueryでクイックトグルリストを書くには?

<script> 
$(document).ready(function() { 
    $('#button_letter_A').click(function() { 
     $('#portfolio_item_A').toggle(); 

    }); 
    $('#button_letter_B').click(function() { 
     $('#portfolio_item_B').toggle(); 
    }); 
}); 
</script> 

すべての文字を順序で書くのではなく、このコードを簡単に書く方法例えば;私はJavaScriptのループでそれを作ることができますか?

答えて

4
// target everything with IDs that start with 'button_letter' 
$("[id^='button_letter']").click(function() { 

    // split the letter out of the ID 
    // of the clicked element and use it to target 
    // the correct div 
    $("#portfolio_item_" + this.id.split("_")[2]).toggle(); 
}); 

参考:

+0

グレートの説明があります。できます。どうもありがとう。 – kalaba2003

0

あなたの命名はちょうどトグル機能を作成し、切り替えることに手紙を渡し一致している場合。手紙を渡すこと

<script> 
$(document).ready(function() { 

    $('#button_letter_'+ letter).click(function() { 
     custom_accordion(letter); 
    }); 
    } 

function custom_accordion(letter){ 
      $('#portfolio_item_'+letter).toggle(); 
} 

}); 
</script> 

コールクリックの機能uがしたい......

このリンクはjqueryのcustom toggle

関連する問題