2013-01-03 53 views
1

我有工作正常的情況如下:jQuery .eq()適用於多個實例嗎?

if (jQuery('#target').val() == '1') { 
    jQuery('.room_row').addClass('hide').eq(0).removeClass("hide"); 
    } 

不過,我需要補充這樣的事,這樣的前2 div.room_row的有他們的「隱藏」類中刪除:

if (jQuery('#target').val() == '2') { 
    jQuery('.room_row').addClass('hide').eq(0,1).removeClass("hide"); 
    } 

UPDATE很多人都在推薦使用切片,但我不能讓它工作。我有一個room_row類的7個div。當您在選擇列表中選擇選項1時,我需要第一個.room_row可見。如果選擇2我需要的第2 .room_row可見,等

回答

1

試試這個

jQuery('.room_row').addClass('hide').slice(0,2).removeClass('hide'); 

而且這將有助於你

jQuery('.room_row:gt(2)').addClass('hide'); 

http://api.jquery.com/slice/

+0

@jdln是您的代碼嗎? – Codegiant

0

你可以用切片:

jQuery('.room_row').slice(2).addClass("hide"); 

這裏有一個簡單的例子:

> [1, 2, 3, 4, 5, 6].slice(2) 
[3, 4, 5, 6] 
> [1, 2, 3, 4, 5, 6].slice(0, 2) 
[1, 2] 

另外,還有:lt:gt

jQuery('.room_row:gt(2)').addClass('hide'); 
0

使用slice inste廣告:

jQuery('.room_row').addClass('hide').slice(0,2).removeClass("hide"); 
0

你試過:

if (jQuery('#target').val() == '2') { 
    jQuery('.room_row:lt(2)').removeClass("hide"); 
    } 
+0

第3行工作,如果我刪除第2行,但您的代碼不會從任何元素刪除隱藏類,因爲它是。 – Evans

+0

爲什麼要添加CLASS後跟removeClass不起作用?我早些時候嘗試過類似的東西,並且遇到同樣的問題。謝謝 – Evans

+0

我不確定... – ATOzTOA