2010-03-20 43 views
1

我正在使用UI和Div創建一個選項卡控件。 UI用於顯示標籤頭。當用戶選擇一個標籤(即,'Li')時,標籤背部顏色相對於其他顏色發生改變。如何使用jQuery從UL獲取所選項目?

我想獲得該UI中的選定而不是選定的li。

我用

$(".tab li:selected").css("background-color","red"); 
$(".tab li:deselected").css("background-color","white"); 

它的不工作,我知道代碼不起作用。只是猜測它。 現在你可以理解我的問題了吧?

回答

6

當用戶選擇一個選項卡,添加一個類來表示此該選項卡:

$('.tab li').click(function() { 
    ... // your existing code 
    $('.tab li').removeClass('selected'); // removes the "selected" class from all tabs 
    $(this).addClass('selected'); // adds it to the one that's just been clicked 
} 

然後,在你的CSS,你可以根據需要對其進行風格。

.tab li { 
    background-color: white; 
} 

.tab li.selected { 
    background-color: red; 
} 
0

當您選擇一個選項卡,你基本上點擊,所以你可以這樣做:

$('.tab li').click(function(){ 
    $(this).css("background-color","red"); 
    $(this).css("background-color","white"); 
}); 

現在$(this)指的是你選擇的選項卡。

0

我會說:

$(".tab li").click(function() { 
    $(".tab li").css("background-color", "white"); 
    $(this).css("background-color", "red"); 
}); 

編輯::selected選擇僅適用於選擇的項目,如<option> S的內部的<select>秒。

相關問題