简体   繁体   English

通过在JavaScript中单击父母的兄弟项目来删除活动的班级

[英]Remove an active class by clicking on a parent's brother item in JavaScript

I am working on a get a card script, that consists of 2 phases. 我正在研究获取卡片脚本,该脚本包含2个阶段。 Selecting a card > opens a relevant window on the right Pressing an Apply now button on that opened window > opens a relevant window on the bottom. 选择卡>在右侧打开一个相关窗口在该打开的窗口上按立即应用按钮>在底部打开一个相关窗口。

Right now everything works as intended, except: I want to make so that if a person removes a card selection (.gacca1 / .gacca2), it also removes the "activated" class from the "Get card" div (.gaccaiGET), thus removing the bottom div (.gara). 现在,一切都按预期工作,除了:我要这样做,以便如果某人删除选择的卡片(.gacca1 / .gacca2),它也从“获取卡片” div(.gaccaiGET)中删除了“激活的”类,因此删除底部的div(.gara)。 Right now, it keeps the "Get a card" activated, even if you deactivate the card selector. 现在,即使您停用卡选择器,它也会保持“获取卡”的状态。

No matter how I try (with my beginner js skills), i can't make it work. 无论我如何尝试(使用我的初学者js技能),我都无法使其正常工作。

Also, if you look at the html and css, you will see that most of the elements are positioned absolute, which is a very bad way of making this work. 另外,如果您查看html和CSS,则会发现大多数元素都是绝对放置的,这是完成此工作的非常不好的方法。 Unfortunately, I could not figure a way to make the positions relative, because the 不幸的是,我无法找到一种使职位相对的方法,因为

.divclass.active + .otherdivclass { css }

works only for brother elements, and, even, only for the brother elements that are next to each other. 仅适用于兄弟元素,甚至仅适用于彼此相邻的兄弟元素。 Which resulted in writing poor html and css, to make it look and work the way I want it to. 这导致编写较差的html和css,使其外观和工作方式符合我想要的方式。 If you have any tips on this matter, please let me know! 如果您对此有任何建议,请告诉我!

Thanks 谢谢

http://jsfiddle.net/4XM9A/10/ http://jsfiddle.net/4XM9A/10/

(function () {
    var links = Array.prototype.slice.call(document.querySelectorAll('.gacca1'));
    var links2 = Array.prototype.slice.call(document.querySelectorAll('.gacca2'));
    var links3 = Array.prototype.slice.call(document.querySelectorAll('.gaccaiGET'));

    var toggleClass = function (className, element) {
        element.classList.toggle(className);
    };

    var clickHandler = function (linksToDisable) {
        return function () {
            toggleClass('active', this);
            linksToDisable.filter(function (link) {
                return link.classList.contains('active');
            })
            .forEach(function (link) {
                toggleClass('active', link);
            });

        };
    };

    links.forEach(function (link) {
        link.addEventListener('click', clickHandler(links2));
    });
    links2.forEach(function (link) {
        link.addEventListener('click', clickHandler(links));
    });
    links3.forEach(function (link) {
        link.addEventListener('click', function () {
            toggleClass('active', this);
        });
    });
}());

As far as I understand you should firstly toggle '.active' of all divs of the '.gaccai' that goes after the ".active" parent. 据我了解,您应该首先在“ .active”父级之后切换“ .gaccai”的所有div的“ .active”。 Try changing function toggleClass a bit: 尝试稍微更改功能toggleClass:

var toggleClass = function (className, element) {
        var array = document.querySelectorAll('.' + element.classList + ' + .gaccai .active');
        for (var i = 0; i < array.length; i++) {
            array[i].classList.toggle(className);
        }
        element.classList.toggle(className);
    };

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

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