2つの異なるソースを使用して詳細行を切り替えようとしています。 2つの異なるトグル機能を使用して同じテーブル行を切り替えます
- ユーザーが名前またはAddressAlertのいずれかをクリックするとは、その具体的な詳細行を示すか、ユーザーがクリックした場合、すべての詳細行が表示または非表示を受ける「すべてを切り替える」
- 隠されます。
問題は、2つの別個のトグル機能が、他のトグル機能が何をしているのかわからないことです。たとえば、「すべてをトグルする」をクリックしただけで、すべての詳細行が表示された場合、個別の名前をクリックするとその詳細行が非表示になります。しかし、個々のトグル機能は「表示」までであるため、その行の詳細を非表示にするには2回のクリックが必要です。
HTML:
<div id="toggleAll"></div>
<table>
<tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
<tr class="details"></tr>
<tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
<tr class="details"></tr>
<tr class="infoRow"><td class="addressAlert"></td><td class="name"></td></tr>
<tr class="details"></tr>
</table>
のjavascript:
//toggles beween showing and hiding the details for specific row
$(
function(){
//if click on carrier name or address alert symbol
$('.name, .addressAlert').toggle(
function() {
//gets the row clicked on, and finds the next row (the details row)
detailsTds = $(this).parents("tr.infoRow").next();
//adds the "shown" class, which sets the display to table-row
detailsTds.addClass("shown");
},
function(){
//gets the row clicked on, and finds the next row (the details row)
detailsTds = $(this).parents("tr.infoRow").next();
//removes the "shown" class, thereby setting the display to none
detailsTds.removeClass("shown");
}
);
}
);
//toggle ALL details
$(
function(){
//if click on carrier name or address alert symbol
$('#toggleAll').toggle(
function() {
$('.details').addClass("shown");
$('#toggleAll').text('[-] Hide all addresses');
},
function(){
$('.details').removeClass("shown");
$('#toggleAll').text('[+] Show all addresses');
}
);
}
);
jqueryの価値があるものについては、自分自身を熟知する価値があるかもしれません。同じことをトグルする2つの事柄を直接手助けすることはありませんが、2つのほぼ同じ機能を持たないクリーナーコードを書くことができます(一般的にadd/removeクラスコールによって違いがあります)。 – Chris