简体   繁体   English

如何通过 knockoutjs 从 html 获取所有 css 类

[英]How to get all css classes from html, through knockoutjs

Im having a problem with selecting all classes with accordion and bringing them into knckoutjs to operate with them.我在使用accordion选择所有类并将它们带入 knckoutjs 以与它们一起操作时遇到问题。

Following this link: https://wpbeaches.com/create-expandcollapse-faq-accordion-collapse-click/ I managed to create nice expandable objects (rectangles), but they are "dead" because Im using knockotjs, and not JS.按照这个链接: https://wpbeaches.com/create-expandcollapse-faq-accordion-collapse-click/我设法创建了很好的可扩展对象(矩形),但它们“死了”,因为我使用的是 knockotjs,而不是 JS。 So my question is how to make it to work?所以我的问题是如何使它工作? First step is that I cant select all accordion classes for some reason..here is my code:第一步是由于某种原因我不能 select 所有accordion类..这是我的代码:

define(['viewmodels/shell', 'durandal/services/logger', 'mediator-js', 'knockout', 'toastr'],
    function (shell, logger, mediator, ko, toastr) {
        var vm = {
            shell: shell,
            activate: activate,
            mediator: mediator
        }
            
        function activate() {
            var acc = jQuery.getElementsByClassName("accordion");
            var panel = document.getElementsByClassName('panel');

            for (var i = 0; i < acc.length; i++) {
                acc[i].onclick = function () {
                    var setClasses = !this.classList.contains('active');
                    setClass(acc, 'active', 'remove');
                    setClass(panel, 'show', 'remove');

                    if (setClasses) {
                        this.classList.toggle("active");
                        this.nextElementSibling.classList.toggle("show");
                    }
                }
            }

            function setClass(els, className, fnName) {
                for (var i = 0; i < els.length; i++) {
                    els[i].classList[fnName](className);
                }
            }
            return true;
        }

        return vm;
    });

What Im trying actually is to copy js part from the link above to my solution and make it work (expand each rectangle..)我实际上试图将 js 部分从上面的链接复制到我的解决方案并使其工作(展开每个矩形..)

It's not quite clear to me what you're trying to do, but the telltale signs in your code indicate that you're tryting to do the wrong thing.我不太清楚您要做什么,但是代码中的迹象表明您正在尝试做错事。

This is the most minimal accordion type of functionality I can think of using Knockout and (for convenience) jQuery, taking the questions and answers straight from the sample page you provided.这是我能想到的使用 Knockout 和(为方便起见)jQuery 的最小手风琴类型的功能,直接从您提供的示例页面中获取问题和答案。

You can always get more sophisticated by adding animations and whatnot.通过添加动画和诸如此类的东西,您总是可以变得更加复杂。 But ultimately, it's about managing a single CSS class across a list of elements.但最终,它是关于跨元素列表管理单个 CSS class 。

 ko.bindingHandlers.cssExclusive = { init: (element, valueAccessor) => { ko.applyBindingsToNode(element, { click: () => { var config = ko.unwrap(valueAccessor()); $(element).closest(config.within).children().not(element).removeClass(config.class); $(element).toggleClass(config.class); } }); } }; const data = { sections: [ {name: "FAQs", items: [ {q: "What currency is the course charged in?", a: "The course is charged in Australian dollars."}, {q: "What if the course doesn't help me?", a: "If it doesn't help you I'll refund the purchase price in full."}, {q: "When will the webinars take place?", a: "Depending on the mix of countries and time zones for people attending the webinars, I will pick a time that works best for most participants. All webinars will be recorded so you can listen to them again. The private Facebook group will obviously be available 24/7 and I'll be monitoring and contributing to the discussion regularly."}, {q: "What is the self-directed mentoring program?", a: "The self-directed mentoring program is designed to help you set-up and run an effective mentee-mentor relationship as part of the course."}, ]} ] }; ko.applyBindings(data);
 .accordion { cursor: pointer; }.panel { display: none; }.accordion.active { color: blue; }.accordion.active +.panel { display: block; color: green; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <div data-bind="foreach: sections"> <h3 data-bind="text: name"></h3> <div data-bind="foreach: items"> <p class="accordion" data-bind="cssExclusive: {class: 'active', within: 'div'}"> <span data-bind="text: 'Q' + ($index() + 1) + '.'"></span> <span data-bind="text: q"></span> </p> <div class="panel">A. <span data-bind="text: a"></span></div> </div> </div>

I can't say how you would integrate that into your sample code, but maybe you can draw some inspiration from it.我不能说你将如何将它集成到你的示例代码中,但也许你可以从中汲取一些灵感。

The core functionality is toggling a CSS class on an element on click.核心功能是在单击元素上切换 CSS class。 For this, I've made a bare-bones custom binding that applies the click binding to the all questions.为此,我制作了一个简单的自定义绑定,将点击绑定应用于所有问题。 The click binding takes care of toggling the active class on the clicked question, and removing it from all others in the same container.单击绑定负责在单击的问题上切换active的 class,并将其从同一容器中的所有其他问题中删除。

This does not even require things like "expanded" observables on the view model, as it keeps its state purely through CSS classes in the DOM.这甚至不需要视图 model 上的“扩展”可观察对象之类的东西,因为它纯粹通过 DOM 中的 CSS 类来保留其 state。

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

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