如何使用jQuery从UL中获取所选项目? [英] How to get the selected item from UL using jQuery?

查看:67
本文介绍了如何使用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;
}

这篇关于如何使用jQuery从UL中获取所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆