0

これが私が持っているコードです:

<a class="clickMe">Text 1</a>
<br />
<div class="sri"> - This text will be toggled</div>

<a class="clickMe">Text 2</a>
<br />
<div class="sri"> - This text will be toggled 2</div>

そしてJquery

$(function () {
    $('a.clickMe').click(function () {
        // find first of following DIV siblings
        // with class "textBox" and toggle it
        $(this).nextAll('div.sri:first').toggle();
    });
});

Text1がクリックされたときに「このテキストはトグルされます」と表示し、「このテキストはトグルされます2」を非表示にすることで正常に動作していますが、反対の方法で動作させたい、つまり「このテキストはトグル」を表示し、Text1 をクリックすると「このテキストはトグル 2」と表示されます。これを修正するのを手伝ってください。

4

3 に答える 3

0

これを試して :

html :

<a class="clickMe a1">Text 1</a>
<br />
<div class="a2"> - This text will be toggled</div>

<a class="clickMe a2">Text 2</a>
<br />
<div class="a1"> - This text will be toggled 2</div>

js:

$(function () {
$('a.clickMe').click(function () {

    var class1 = $(this).attr("class").split(" ")[1];
    $("div." + class1).toggle();
});
});
于 2012-08-14T06:54:06.363 に答える
0

なぜ追加のクラスが必要なのか、これで質問が解決します

    $('a.clickMe').click(function () {
        // find first of following DIV siblings
        // with class "textBox" and toggle it
$(this).nextAll('div.sri:first').show();        
$('div.sri:visible').not($(this).nextAll('div.sri:first')).toggle();

    });​

ライブデモ

于 2012-08-14T06:56:01.567 に答える
0

これを使って

$(function () {
$('a.clickMe').click(function () {
    // find first of following DIV siblings
    // with class "textBox" and toggle it
       $('div.sri').eq(1).toggle();
});
});
于 2012-08-14T06:41:08.067 に答える