简体   繁体   English

完成一个手风琴 jQuery 功能,我尝试了很多,似乎没有任何效果?

[英]Completing an accordion jQuery feature, I've tried a lot, nothing seems to work?

I'll get straight to the point, I'm trying to complete this accordion effect (when you open one, the others close) and there is about 7 attributes on the accordion.我将切入正题,我正在尝试完成这种手风琴效果(当您打开一个时,其他人关闭)并且手风琴上大约有 7 个属性。 Each one has a PLUS sign so when you open that it should turn into a MINUS sign (via Font Awesome, this part I can take care of, just for context).每个都有一个加号,所以当你打开它时,它应该变成一个减号(通过 Font Awesome,这部分我可以处理,只是为了上下文)。

HTML The <a> contains the icon which is a + sign and the ul.checkbox-list is what needs to be monitored for whether or not it has an active class so it can collapse the others. HTML <a>包含一个+符号的图标, ul.checkbox-list是需要监视它是否具有active类的内容,以便它可以折叠其他类。 I've gotten somewhat close but then something else happens, I thought maybe another set of eyes could help?我已经有点接近但后来发生了其他事情,我想也许另一组眼睛会有所帮助?

 $(document).ready(function(){ $("a[id^='attr']").on("click", function() { if( $(this + "ul.checkbox-list").hasClass("visible") ) { // Got lost as to what to do here .. } else { $("a[id^='attr'] > i").removeClass("fa-plus-circle").addClass("fa-minus-circle"); $("ul.checkbox-list").removeClass("active"); $(this).addClass("active"); $("ul.checkbox-list").slideUp(200); $(this).siblings().slideDown("slow"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <section> <ul id="att7"> <li> <a href="#" id="attr7"><i class="fas fa-plus-circle"> </i> Lumens</a> <ul class="checkbox-list"> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Antique Nickel</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Black & Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> </ul> </li> </ul> <!-- This is the last attribute in the accordion --> </section> </div>

EDIT: The accordion (when opened, contains checkboxes FYI).编辑:手风琴(打开时,包含复选框仅供参考)。

There are a multitude of mistakes in your code.你的代码中有很多错误。 First of all $(this + "ul.checkbox-list") does not work.首先$(this + "ul.checkbox-list")不起作用。 This is an element, you cannot concat it to a selecor.这是一个元素,您不能将其连接到 selecor。 Use $(this).next("ul.checkbox-list") to find the next elemen (IF it has that class. Then you mixed up some cases, showing the siblings etc. Your active class and visible class seemed unrelated (though I guess the purpose was to have one on the button the other on the list). In the code I modified I condensed it down to just active class:使用$(this).next("ul.checkbox-list")找到下一个元素(如果它有那个类。然后你混淆了一些情况,显示兄弟姐妹等。你的活动类和可见类似乎无关(虽然我想目的是让一个按钮在列表中,另一个在列表中。在我修改的代码中,我将其压缩为活动类:

 $(document).ready(function(){ $("a[id^='attr']").on("click", function(event) { event.preventDefault(); if( $(this).next("ul.checkbox-list").hasClass("active") ) { $(this).next("ul.checkbox-list").slideUp(200).removeClass("active"); } else { $("a[id^='attr'] > i").removeClass("fa-minus-circle").addClass("fa-plus-circle"); $(this).find(" > i").removeClass("fa-plus-circle").addClass("fa-minus-circle"); $("ul.checkbox-list").removeClass("active"); $(this).siblings('ul.checkbox-list').slideUp("slow"); $(this).next("ul.checkbox-list").slideDown(200).addClass("active"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <ul id="att7"> <li> <a href="#" id="attr7"><i class="fas fa-plus-circle"> </i> Lumens</a> <ul class="checkbox-list active"> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Antique Nickel</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Black & Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li style="display: flex; flex-direction: row;"> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> </ul> </li> </ul> <!-- This is the last attribute in the accordion --> </section> </div>

several mistakes on your code:您的代码中有几个错误:

#1 $(this + "ul.checkbox-list") you could simply do a console.log(this) to know that this is not a string, as the + is a abreviation for concat() as you would most likely end up doing #1 $(this + "ul.checkbox-list")你可以简单地做一个console.log(this)来知道this不是一个字符串,因为+concat()的缩写,因为你很可能会结束做

$("[object HTMLDocument]ul.checkbox-list")

#2 this code #2这段代码

.slideUp(200).removeClass("active")

is the same as是相同的

.removeClass("active").slideUp(200)

as the slide as you can see from the documentation, is an async function, not synchronous, so it will execute the chain right away, and not wait for anything ... to remove the class only when the slide animation ends, you would need to pass it as a callback正如您从文档中看到的那样, slide是一个异步函数,而不是同步函数,因此它将立即执行链,而不是等待任何事情……仅在幻灯片动画结束时才删除该类,您需要将其作为回调传递

.slideUp(200, () => ...removeClass("..."))

#3 you are checking for a visible class, but in all your code, all you do is handle an active class instead... #3你正在检查一个visible类,但在你所有的代码中,你所做的只是处理一个active类......


all in all, I would fix and refactor your code into something like this:总而言之,我会将您的代码修复并重构为如下所示:

all lines explained for better understanding解释所有行以更好地理解

 $(function() { $("a[id^='attr']").on("click", onClickLink); }); function onClickLink(evt) { evt.preventDefault(); // prevent browser to jump up var elm = $(evt.delegateTarget), // the jquery traget elemnet chb = elm.next("ul.checkbox-list"), // the next ul.checkbox-list isActive = chb.hasClass("active"); // if the next element is active minimizeAll(() => { // reset to default position if(!isActive) maximizeGroup(elm); // maximize only if the clicked elm is not active }); } // minimize all elements function minimizeAll(cb) { if($("ul.checkbox-list.active").length === 0 && cb) // prevent slide up when all are minimized return cb(); // if callback is passed, use it $("a[id^='attr'] > i") // find all .addClass("fa-plus-circle") // add PLUS .removeClass("fa-minus-circle"); // remove MINUS $("ul.checkbox-list").slideUp("slow", () => { // start sliding up $("ul.checkbox-list").removeClass("active"); // when sliding up ends, remove active class if(cb) cb(); // if callback is passed, use it }); } // maximize only the element group function maximizeGroup(elm) { elm.next("ul.checkbox-list").stop().slideDown(200, () => { // STOP any previous animation and start sliding down the selected group elm.find("> i") // from the clicked element (<a>) find (<i>) .removeClass("fa-plus-circle") // remove PLUS .addClass("fa-minus-circle"); // add MINUS elm // from the clicked element (<a>) .next("ul.checkbox-list") // select next ul.checkbox-list .addClass("active"); // and add "active" class }); }
 a[id^='attr'] { text-decoration: none; color: #333; } ul li { list-style: none; } /* default values */ ul ul { display: none; } ul ul > li { display: flex; flex-direction: row; } ul.checkbox-list.active { display: flex; flex-direction: column; }
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.1/css/all.css" integrity="sha384-v8BU367qNbs/aIZIxuivaU55N5GPF89WBerHoGA4QTcbUjYiLQtKdrfXnqAcXyTv" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <section> <ul id="att7"> <li> <a href="#" id="attr7"><i class="fas fa-plus-circle"></i> Lumens 1</a> <ul class="checkbox-list"> <li> <div class="roundedTwo" style=""> <p>Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Antique Nickel</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Black &amp; Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> </ul> </li> </ul> <!-- This is the last attribute in the accordion --> </section> <section> <ul id="att8"> <li> <a href="#" id="attr8"><i class="fas fa-plus-circle"></i> Lumens 2</a> <ul class="checkbox-list"> <li> <div class="roundedTwo" style=""> <p>Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Antique Nickel</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Black &amp; Aged Brass</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> <li> <div class="roundedTwo" style=""> <p>Titanium</p> <input type="checkbox" value="None" id="roundedTwo" name="check" checked=""> <label for="roundedTwo"></label> </div> </li> </ul> </li> </ul> <!-- This is the last attribute in the accordion --> </section> </div>

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

相关问题 我一直没有尝试过让Magnific Popup正常工作 - Nothing I've been trying seems to get the Magnific Popup to work properly 我尝试了一些 ajax 请求,但它不起作用 - I've tried to some the ajax request but it won't work Javascript/Jquery:代码不重复我尝试过的任何循环 - Javascript/Jquery: code not repeating with any loop I've tried 无法计算表格行的总和我从互联网上尝试了很多方法但没有 - Can't calculate the sum of table rows I've tried many ways from the internet but nothing 无法将vimeo视频与fancybox配合使用-我觉得我已经尝试了所有方法 - Unable to get a vimeo video to work with fancybox— I feel like I've tried everything 如何在CSS上延迟动画? 我尝试了动画延迟。 不起作用 - How to delay animation on CSS? I've tried animation-delay. Doesn't work 如何调用方法 myFunction 返回(名称)我已经尝试了很多方法但它不起作用 - How to invoke the method myFunction to return (name) i've tried many ways but it doesn't work jQuery添加一个类-我尝试过的所有方法均会在单击时删除该类 - jQuery adding a class - all methods I've tried remove the class on click jQuery手风琴不起作用 - jQuery accordion does not work 当代码看起来不错时,JQuery Accordion无法正常工作 - JQuery Accordion not working when code seems alright
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM