简体   繁体   English

有没有更优雅的方法将 css 应用于“除此之外的所有元素”?

[英]Is there a more elegant way to apply css to “all elements but this one”?

I have some html/jQuery that I am using to implement some tabbed navigation.我有一些 html/jQuery 用于实现一些选项卡式导航。 When a new tab is selected I need to make it's css class "active" and change the previous tabs css class to "inactive".选择新选项卡时,我需要将其设置为 css class “活动”,并将以前的选项卡 css ZA2F2A29F8EBC0ABCBBDC 更改为“Ainactive”。 To do this I have been using the following code inside of my click event:为此,我在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.

This works but I'm curious if anyone has found a cleaner/more elegant way of doing this?这行得通,但我很好奇是否有人找到了一种更清洁/更优雅的方法?

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

Working example at: http://jsfiddle.net/4uMmc/工作示例: http://jsfiddle.net/4uMmc/

Why don't you just have the inactive style be the default style?为什么不将inactive样式设置为默认样式? Then, you can just do this:然后,您可以这样做:

$("ul.tabs li").removeClass("active");
$(this).addClass("active");

$(this).removeClass("inactive").addClass("active").siblings().removeClass("active").addClass("inactive");

Note, that it's better not to use $('ul.tabs li') in element's click event, so if you'd have many .tabs only the one where the clicked element is would be affected.请注意,最好不要在元素的click事件中使用$('ul.tabs li') ,因此如果您有许多.tabs ,则只有单击元素所在的那个会受到影响。

However, it's better to write CSS so you won't have to use inactive class, like this:但是,最好编写 CSS,这样您就不必使用inactive class,如下所示:

ul.tabs li {…} /* Inactive state */
ul.tabs li.active {…} /* Active state */

Why not simply find elements with that class and then remove it?为什么不简单地找到具有该 class 的元素,然后将其删除? You could simplify that down to two lines:您可以将其简化为两行:

$('.active').removeClass('active').addClass('inactive');
$(this).addClass('active').removeClass('inactive');

This at least only evaluates the selectors once.这至少只评估选择器一次。

$("ul.tabs li").removeClass("active").addClass("inactive");
$(this).removeClass("inactive").addClass("active");

I agree with other suggestions, it's better to just get rid of the inactive class.我同意其他建议,最好摆脱不活动的 class。 Make the default formatting for the tab be "inactive".使选项卡的默认格式设置为“非活动”。 Then, just add/remove the active class as needed.然后,只需根据需要添加/删除活动的 class。 Put the CSS for the "active" class after the default CSS for the tabs and it will supercede the default state.将“活动”class 的 CSS 放在选项卡的默认 CSS 之后,它将取代默认的 Z697339E5EA9EZ25A8。 Then, your code could just be this:然后,您的代码可能是这样的:

$("ul.tabs li.active").removeClass("active");
$(this).addClass("active");

Using :not() is way faster than .not() Also instead of adding and removing classes use jQuery toggleClass() You should write the code just like this:使用:not().not()快得多 此外,不要添加和删除类,而是使用 jQuery toggleClass()您应该像这样编写代码:

    $("ul.tabs li:not(" + $(this) + ")").toggleClass("active", "inactive");
$(this).addClass('active');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM