ボタンとして使用している多数の画像を含むjQueryモバイルページがあります。
ページが最初に読み込まれるとき、すべての画像のクラスは「非アクティブ」です。ボタンの 1 つをクリックすると、そのクラスが「アクティブ」に変更され、他のすべてのボタンが「非アクティブ」から「無効」に変更されます。ページから離れてまっすぐ戻ると、1 つの画像にはまだ「アクティブ」のクラスがあり、残りの画像にはまだ「無効」のクラスがあります。
アクティブなボタンをクリックすると、クラスが「アクティブ」から「非アクティブ」に変更され、他のすべてのボタンのクラスが「無効」から「非アクティブ」に変更されます。これにより、別のボタンを選択してアクティブにすることができます。
以前は無効だったが現在は非アクティブになっているボタンの 1 つをクリックすると、クリック イベントが発生しません。ページが最初にロードされたときと同じように、クラスがまだ無効になっていると考えているかのようです。Firefox で Firebug を使用して確認したところ、クラスは正しく非アクティブに設定されていますが、クラスが無効になっているかのように動作しています。
これが私のjQueryです:
$(document).ready(function() {
$('.inactive, .active').on("click", function(){
// Toggle active/inactive class
$(this).toggleClass("active inactive");
// Disable if 1 active button, else inactive
if($('.active').length == 1) {
$('.inactive').toggleClass("inactive disabled");
} else {
$('.disabled').toggleClass("disabled inactive");
}
});
});
そして HTML ボタン:
<a class="button inactive"></a><br />
<a class="button inactive"></a><br />
<a class="button inactive"></a><br />
<a class="button inactive"></a><br />
<a class="button inactive"></a><br />
<a class="button inactive"></a>
そしてクラス:
.button {
background:url("../images/compare.png") no-repeat;
width: 18px;
height:18px
}
.active {
background-position: 0 -18px;
cursor:pointer
}
.inactive {
background-position: 0 0;
cursor:pointer
}
.disabled {
background-position: 0 -36px;
cursor:default
}