简体   繁体   English

jQuery菜单 - addClass X removeClass Y和Z.

[英]jQuery Menu - addClass X removeClass Y and Z

I have a List for a Menu and I want to Add or Remove a class to the Item Link, as well as hide previous applied classes to the same list applying only the class to the clicked Item. 我有一个菜单列表,我想在项目链接中添加或删除一个类,以及将以前应用的类隐藏到同一列表中,仅将该类应用于所单击的项目。
In addition I also want to display a hidden div for the item, hiding any div triggered by the click on any of the other items. 此外,我还想显示项目的隐藏div,隐藏任何其他项目点击触发的div。

This is working for me, but I feel like I'm missing something that will make it simpler, maybe I'm wrong, or maybe I'm not, I'd like to know your opinion and suggestions. 这对我有用,但我觉得我错过的东西会让事情变得更简单,也许我错了,或者我不是,我想知道你的意见和建议。

$("ul li:first-child").click(function(){
    $("ul li:first-child").toggleClass('lihover');
    $("ul li:nth-child(2), ul li:nth-child(3)").removeClass('lihover');
    $(".item1").fadeToggle();
    $(".item2, .item3").hide();
});

Basically the fiddle will talk for myself. 基本上小提琴会为自己说话。
Please Check it for a better understanding. 请检查它以便更好地理解。 http://jsfiddle.net/fyjHk/1/ http://jsfiddle.net/fyjHk/1/

Keep in mind, this is working as intended, I just want to know if there's any other way to do this with less steps (code). 请记住,这是按预期工作的,我只是想知道是否有任何其他方法可以用更少的步骤(代码)来做到这一点。 This because, imagine if I have a menu with 20 Items, list them all to hide and/or show is a bit polluted in the code IMO, so maybe there's a way to simplify this. 这是因为,想象一下,如果我有一个包含20个项目的菜单,列出它们全部隐藏和/或显示在代码IMO中有点污染,那么也许有一种方法可以简化这一点。

Keep in mind I'm new to jQuery and Javascript, take this with a bit of salt. 请记住,我是jQuery和Javascript的新手,请稍微考虑一下。

Thank you in advance 先感谢您

Personally, I'd go with this solution: http://jsfiddle.net/fyjHk/10/ . 就个人而言,我会选择这个解决方案: http//jsfiddle.net/fyjHk/10/

It reuses much more code, keeping things clean. 它重用了更多的代码,保持了清洁。 It also utilizes some handy jQuery functions to make things more compact and generic. 它还利用一些方便的jQuery函数来使事情更紧凑和通用。

HTML: HTML:

<ul>
    <li data-id='item1'>item 1</li>
    <li data-id='item2'>item 2</li>
    <li data-id='item3'>item 3</li>
    <li>hide all</li>
</ul>

<div id="item1" class="item hide">Content For Item 1</div>
<div id="item2" class="item hide">Content For Item 2</div>
<div id="item3" class="item hide">Content For Item 3</div>

By letting the li elements hold the information about which div they are coupled with, you save a lot of JS code and make it easier to add and remove items. 通过让li元素保存有关它们与哪个div的信息,可以节省大量JS代码并使添加和删除项更容易。 Also, to identify one element its always best to use an id and use a class for a group of elements who share a certain logic. 此外,要识别一个元素,最好使用id并将一个class用于共享某个逻辑的一组元素。 In this case, all div s items, but also have an id to be coupled with the li . 在这种情况下,所有div的项目,但也有一个id与li

Javascript: 使用Javascript:

$("ul li[data-id]").click(function(){
    $(this).toggleClass('lihover').siblings().removeClass('lihover');
    $("#" + $(this).data("id")).fadeToggle().siblings(".item").hide();
});

$("ul li:last-child").click(function(){
    $(".item").hide();
    $("ul li[data-id]").removeClass('lihover');
});

One of the nice things about letting the li elements hold the info is that you can cut down on code duplication and write cleaner selectors. li元素保存信息的一个好处是你可以减少代码重复并编写更干净的选择器。 The siblings function, you can easily select the other elements instead of having to define them all in your code. siblings功能,你可以轻松选择其他元素,而不必在你的代码中定义它们。

You could avoid all the duplication, by doing something like this: 通过这样做,你可以避免所有的重复:

$("ul li").on('click', function(){
    $('.items div').fadeOut(0);    
    $('.items div').eq($(this).index()).fadeIn(300);
});

You'll probably have to edit it a bit to add and remove the active class. 您可能需要稍微编辑它以添加和删除活动类。

JSFiddle Demo JSFiddle演示

Easiest i could see: 最简单的我能看到:

$("ul li").click(function(){
    var _this = $(this);
    var index = _this.index();

    // Reset
    var hover = _this.hasClass('lihover');
    $('ul li').removeClass('lihover');

    if(_this.hasClass('hide-all')) {
         $('.item').hide();   
    } else {
        if (!hover) {
           _this.addClass('lihover');
        }
        $('.item').eq(index).fadeToggle();
        $('.item').eq(index).siblings('.item').hide();
    }
});

http://jsfiddle.net/fyjHk/4/ new fiddle: http://jsfiddle.net/fyjHk/12/ http://jsfiddle.net/fyjHk/4/新小提琴: http//jsfiddle.net/fyjHk/12/

$("ul li").click(function(){
    var li = $(this).parent().children(),
        sel = li.index($(this)) + 1;
    if(sel == 4)
        return $('.hide').hide();
    li.each(function(i){
        $('.item' + i)[i == sel ? "fadeIn" : "hide"]();
    });
});

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

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