javascript - 有没有更优雅的方法将 css 应用于 "all elements but this one"?

标签 javascript jquery html css

我有一些 html/jQuery 用于实现一些选项卡式导航。选择新选项卡后,我需要将其 css 类设置为“事件”并将之前的选项卡 css 类更改为“非事件”。为此,我一直在我的 click 事件中使用以下代码:

$("ul.tabs li").removeClass("active"); //Remove any "active" class
$("ul.tabs li").addClass("inactive");  //Set all to "inactive"
$(this).removeClass("inactive");  //remove "inactive" from the item that was clicked
$(this).addClass("active"); //Add "active" class to the item that was clicked.

这行得通,但我很好奇是否有人找到了更简洁/更优雅的方法?

最佳答案

$("ul.tabs li").click(function(){
    $(".active").removeClass("active").addClass("inactive");
    $(this).removeClass("inactive").addClass("active");
});

工作示例位于:http://jsfiddle.net/4uMmc/

关于javascript - 有没有更优雅的方法将 css 应用于 "all elements but this one"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7003041/

相关文章:

javascript - 使用 jQuery 禁用/启用输入?

css - 修复 Chrome 中视频重叠的问题

javascript - SpagoBI:使用查询数据集结果的脚本数据集

javascript - 如何使用 JSON Schema 验证表单?

JavaScript:谷歌地图mapOptions?

jquery - 使用 jQuery 和 GreaseMonkey 检测 Gmail 何时完成加载

javascript - 无法在使用 ajax 的 div 中加载 HTML

javascript - jQuery DataTables 功能不可用

javascript - 标签和字段在同一行的 ExtJS 表单

javascript - Rails/Stripe - 付款不会处理