このHTMLを考えると:
<div id="foo">
<input type=button class="foo abtn_1">
<input type=button class="joe bbtn_2">
<input type=button class="doe cbtn_2">
<input type=button class="cool dbtn_1">
</div>
ボタンをクリックすると、アンダースコア(のみ)が含まれているクラスを取得し、その番号を2から1に変更する必要があります。
現在、私はこれを各クラスに使用しています。
$('.abtn_1').live('click', function () {
$(this).switchClass('abtn_1','abtn_2');
});
$('.abtn_2').live('click', function () {
$(this).switchClass('abtn_2','abtn_1');
});
$('.bbtn_1').live('click', function () {
$(this).switchClass('bbtn_1','bbtn_2');
});
$('.bbtn_2').live('click', function () {
$(this).switchClass('bbtn_2','bbtn_1');
});
// etc
そのため、これを何度も行う代わりに、アンダースコアクラスを取得して、その数を1、2行にまとめ、それに応じてその数を変更することを考えました。
私はそれが次のようになると想像します:
var a = // $(this).attr('class') without number and underscore
var b = // This class number only
var c = $(this).attr('class'); // If I wanted to call it from
// its parent $('#foo input').
if a contains number 1 {
$(this).switchClass(c, a + '_1')
} else {
$(this).switchClass(c, a + '_2')
};