1

多数のロジック チェックに基づいて、多数の dom 要素の可視性を制御する必要があります。jquery で次のようなことが本当に必要です。

$('.FirstPanel').visible = (condition == 1 || othercondition == true);
$('.SecondPanel').visible = (condition > 3 || othercondition == false);
$('.ThirdPanel').visible = (condition < 3 || stillanothercondition = 'asdf');
etc

もちろん、if または switch ステートメントを使用して上記のコードを表現して$('.FirstPanel').hide()... を呼び出すことはできますが、それには何倍ものコード行数が必要になります。

if(condition == 1 || othercondition == true) {
    $('.FirstPanel').show();
    $('.SecondPanel').hide();
    $('.ThirdPanel').hide();
} else if (condition > 3 || othercondition == false) {
    $('.FirstPanel').hide();
    $('.SecondPanel').show();
    $('.ThirdPanel').hide();
} etc

明らかな何かが欠けているように感じます。ありがとう


2012.09.24 更新

皆さんの回答に感謝します-トグルメソッドはチケットです(以下のMichaëlWitrantの受け入れられた回答を参照してください)

4

2 に答える 2

3
$('.FirstPanel').toggle(condition == 1 || othercondition == true);
$('.SecondPanel').toggle(condition > 3 || othercondition == false);
$('.ThirdPanel').toggle(condition < 3 || stillanothercondition = 'asdf');

http://api.jquery.com/toggle/

于 2012-09-13T17:58:04.247 に答える
0

次のようなものを試してください:

$(".element").css("display", function() { value = (condition == 1 || othercondition == true); if (value) { return 'block'; /*or 'inline' or 'inline-block'*/ } 
return 'none'; });
于 2012-09-13T17:55:26.600 に答える