如何使用jQuery从UL中获取所选项目?

问题描述:

我正在使用Ul和Divs创建一个选项卡控件. Ul用于显示标签头.当用户选择一个选项卡(即"Li")时,选项卡的背景色相对于其他选项卡将发生更改.

I am creating a tab control using Ul and Divs. Ul is used to display the tab heads. When the user selects one Tab (ie , 'Li') ,the tab back color get changed with respect to others.

我想在该Ul中获取选定的"而不是未选定的li.

I want to get the Selected and not selected li in that Ul .

我用过

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

这是行不通的,我知道代码行不通.请猜测一下. 现在您可以理解我的问题了吧?

It 's not working, I know the code does not work .just guess it. Now may you understood my problem,right?

当用户选择一个选项卡时,请向该选项卡添加一个表示以下内容的类:

When your user selects a tab, add a class to that tab that represents this:

$('.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中根据需要设置样式.

Then, in your CSS, you can style it as necessary.

.tab li {
    background-color: white;
}

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