2011-12-11 14 views
1

は、私は、このHTMLがある場合:今、私はtoggleClassアクションにonclickの親1のためのjQueryを必要とトグルdivの声明

$(".article").click(function() { 
    $(this).find(".action").toggleClass("collapse"); // works onclick div article 
    ????????? // when press parent1 if parent2 pressed before ???????.find(".action").toggleClass("collapse"); 
    $("#exp2").hide(); // if previously open 
    $("#exp1").toggle(300); 
} 

<div id="parent1" class="article"> 
    <div class="action"></div> 
    <div id="exp1" class="expand"></div> 
</div> 
<div id="parent2" class="article"> 
    <div class="action"></div> 
    <div id="exp2" class="expand"></div> 
</div> 

とCSS:

.article:hover .action { 
    z-index:1000; 
} 
.expand { 
    display: none; 
} 
action { 
    background: url(../images/down.png) 0 0 no-repeat; 
    z-index: -999; 
} 
.collapse { 
    background: url(../images/up.png) 0 0 no-repeat !important; 
    z-index: 1000; 
} 

とJavascriptを親権者2が崩壊するために前もって切り替えられている場合のみ。

+1

をしますか唯一の.article.active

  • のがあるでしょうz-index = 1000; 'は有効なCSSではありません。 'z-index:1000;'でなければなりません。 –

  • +0

    注釈ありがとう – user1080711

    答えて

    0

    これにはいくつかの方法があります。通常、私はドロップダウン(ブール値)の状態を保持するローカル変数を格納し、それをチェックするだけです。 jQueryを使って

    、すでに)(非表示を使用しているので、あなたが使用することができます。

    if ($("#exp2").is(":hidden")) { 
        // Show content, set arrow to up. 
        $("#exp2").removeClass("expand").addClass("collapse"); 
    } 
    else { 
        // Hide content, set arrow to down. 
        $("#exp2").removeClass("collapse").addClass("expand"); 
    } 
    

    トグルをあなただけの追加やクラスを削除した場合は、より理にかなって。ここでは交互のクラスです。 addClass()とremoveClass()を使用すると、何をしているのかが非常に明確になります。

    +0

    parent2 $(#parent2).find( "。action")に対してこの関数をonclick parent1と呼ぶにはどうすればいいですか?toggleClass( "collapse"); – user1080711

    +0

    私はあなたが求めていることを理解していますが、それは本当にそれをもっと混乱させようとしています。交互の場合、両方のonclickイベントによって呼び出される1つのメソッドですべてを処理できます。私はjQueryが意図していたことを知っていますが、機能は少し高尚だと思います。 – Xcalibur37

    +0

    私はこのようなことをしました\t \t \t \t \t \t(私はこのような何かをしました。ます。

    0

    私はここで推測しますが、開いた.articleを閉じて、クリックしたものを開いてみたいと思います。

    $(".article").click(function() { 
        // see explanations 1 and 2 below 
        $('.article.active').removeClass('active').find('#exp1').hide(); 
    
        // see explanation 3 below 
        $(this).addClass('active').find('#exp2').show(); 
    } 
    

    あなたのニーズに合わせてコードを変更することができますが、あなたはそれを理解:

    1. 我々は.articleをクリックすると、我々は(特定のクラスを持つ他の.articleため.active探します私の例では)。技術的には、おそらく `、我々はその.activeクラスを削除し、何それか、その子の
    2. 我々は最終的に1のために何でもクリックした記事の