2012-07-24 38 views
1

我有3個 「跨度」 S我不能隱藏/顯示jQuery中的一些元素

<span class="input1" style="display:block;"><span class="round"></span></span> 
<span class="input2" style="display:none;"><span class="round"></span></span> 
<span class="input3" style="display:none;"><span class="round"></span></span> 

這是腳本:

$(document).ready(function() { 
    act = 1; 
    next = act+1; 
    }); 

$(".round").click(function(){ 
     $('.input'+act).hide(); 
     $('.input'+next).show(); 
     act = next; 
    }); 

推 「.round」 首次 - >「.input1」 - 隱藏&「.input2」 - 顯示 如果我想第二次推「.round」 - >什麼也沒有發生。 (這是問題)

如何隱藏「.input2」並顯示「.input3」? 感謝您的幫助和對我的英語感到抱歉!

回答

1

由於next總是等於2。

第二次,act == 2next == 2所以隱藏然後顯示相同的元件。

試一下:

$('.round').click(function(){ 
    $('.input' + act).hide(); 
    $('.input' + next).show(); 
    act = next++; 
}); 

的解釋

根據MDN了一下:

++是遞增運算符。

此運算符會增加(加1)其操作數並返回 值。如果使用後綴,操作符後面有運算符(例如, x ++),則它會在增加前返回該值。如果前綴 與操作數之前的運算符(例如,++ x)一起使用,則在遞增後返回 值。

+0

謝謝GG。 ! – 2012-07-24 23:37:56

2

我可能不會嘗試通過特定的類名進行操作...使用DOM結構。

$(".round").click(function(){ 
    $(this).parent().hide().next().show(); 
}); 

的jsfiddle:http://jsfiddle.net/U7cYN/

+0

有趣的是,這不起作用... – Purag 2012-07-24 23:35:27

+0

http://jsfiddle.net/U7cYN/ - 是的,它的確如此。 – ahren 2012-07-24 23:37:20

+0

http://jsfiddle.net/purmou/6RFGg/那麼我的是什麼? 0.o – Purag 2012-07-24 23:40:20

0

這將工作,在第三次點擊文本將顯示dispear:

$(document).ready(function() { 
    act = 1; 
    next = act; 
}); 

$(".round").click(function(){ 
    next++; 
    $('.input'+act).hide(); 
    $('.input'+next).show(); 
    act = next; 
});