简体   繁体   English

如何删除除一个以外的所有类

[英]How to remove all classes except one

I'm trying to remove the class 'fa-minus' unless its part of the element being triggered. 我正在尝试删除类“ fa-minus”,除非它被元素的一部分触发。 Everything else is working great, however, I'm having trouble targeting all the spans that have the class 'fa-minus' without affecting the dropdown being clicked. 其他所有工作都很好,但是,我无法定位所有具有“ fa-minus”类的跨度,而不会影响单击的下拉列表。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

HTML HTML

<div id="accordion">
        <h3 class="accordion-trigger">Do you lease to individuals and corporations?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>Cornerstone leases to all people who are in need of short term furnished housing:  business travelers, human resource/relocation departments, government personnel, and vacation travelers.  We also serve the insurance industry for displaced home owners in need of temporary furnished housing.</p>
      </div>


      <div class="hr1"></div>

      <h3 class="accordion-trigger">Are you the same as an extended stay hotel?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>No. We offer a full sized furnished apartment that is much larger than a hotel room and is fully equipped with all the comforts of home.</p>
      </div>

      <div class="hr1"></div>

      <h3 class="accordion-trigger">Is there a minimum stay?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>Yes, most locations have a 30 day minimum length of stay.</p>
      </div>
      <div class="hr1"></div>

      <h3 class="accordion-trigger">Can I bring my family pet?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>Yes you can, as we offer pet friendly properties in every city and area.  Most properties have a pet weight limit and breed restrictions, and require an additional pet deposit and/or pet fee.</p>
      </div>
      <div class="hr1"></div>

      <h3 class="accordion-trigger">How do I Pay?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>We accept Visa, MasterCard and American Express.  For companies paying by check, a major credit card is required on file.</p>
      </div>

      <div class="hr1"></div>

      <h3 class="accordion-trigger">Is there a fee for internet access?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>No, our apartments include high speed internet and Wi-Fi service at no additional charge.</p>
      </div>

      <div class="hr1"></div>

      <h3 class="accordion-trigger">Is phone service included?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>We have found that most of our guest use their cell phones and do not require land lines.  For additional questions please speak with a Cornerstone representative.</p>
      </div>

      <div class="hr1"></div>

      <h3 class="accordion-trigger">How do I get the keys to my apartment and check in?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>Once your reservation is confirmed we will send you your address and easy directions from the airport or major highway.  Typically guest should plan to arrive during office hours.  Late check in can be arranged but may require an additional fee.</p>
      </div>

      <div class="hr1"></div>

      <h3 class="accordion-trigger">What are the check in and check out times?<span class="fa fa-fw fa-plus icons accordion-icon"></span></h3>
      <div class="accordion-content-container">
        <p>Typically check in is at 2pm and check out is at 11am.</p>
      </div>

    </div> <!-- accordion end -->

JS JS

$(document).ready(function($) {
 $('#accordion').find('.accordion-trigger').click(function(){

 //Expand or collapse this panel

 $(this).next().slideToggle('fast');
 $(this).find('span').toggleClass('fa-minus');

 //Hide the other panels

 $(".accordion-content- container").not($(this).next()).slideUp('fast');

 // remove classes 'fa-minus' unless its part of the accordion being triggered

 });
});

Something like this should get you started: 这样的事情应该让您入门:

jsFiddle Demo jsFiddle演示

$(".accordion-content-container").hide();

$('.accordion-trigger').click(function() {
   //Expand or collapse this panel
   $(this).next().slideToggle('fast');
   $(this).find('span').toggleClass('fa-minus');

   //Hide the other panels
   $(".accordion-content-container").not($(this).next()).slideUp('fast');

   // remove classes 'fa-minus' unless its part of the accordion being triggered
    $('span').removeClass('fa-minus');
   $(this).find('span').addClass('fa-minus');
});

Notes: 笔记:

(1) You can just use $('.accordion-trigger').click instead of $('#accordion').find('.accordion-trigger').click (1)您可以只使用$('.accordion-trigger').click代替$('#accordion').find('.accordion-trigger').click

(2) I added $(".accordion-content-container").hide(); (2)我添加了$(".accordion-content-container").hide(); in javascript to initially hide all content divs. 在javascript中最初隐藏所有内容div。

Thanks, for all the help. 谢谢您的帮助。 I ended up wrapping each section of the accordion in a container to better utilize the available JQuery methods. 我最后将手风琴的每个部分包装在一个容器中,以更好地利用可用的JQuery方法。 I appended, .parent().find('span').removeClass('fa-minus'); 我附加了.parent()。find('span')。removeClass('fa-minus'); to $(".accordion-content-container").not($(this).next()).slideUp('fast'). 到$(“。accordion-content-container”)。not($(this).next())。slideUp('fast')。 This is what I was trying to do in the first place, I just didn't read the documentation close enough and was trying to use 'parent()' in the wrong way. 这是我最初尝试做的事情,只是我没有足够仔细地阅读文档,并试图以错误的方式使用'parent()'。

  1. $(".accordion-content-container").not($(this).next()).slideUp('fast') $(“。accordion-content-container”)。not($(this).next())。slideUp('fast')

This line of code was already where I needed to be to remove the 'fa-minus' class, I was just having a hard time traversing back up the DOM to the span with the class of 'fa-minus'. 这行代码已经是删除“ fa-minus”类所需的地方,我只是很难遍历使用“ fa-minus”类将DOM返回到跨度。

  1. Wrapping each section of the accordion in a 'acccordion-container' div gave the ability to use parent(). 将手风琴的每个部分包裹在“ acccordion-container” div中可以使用parent()。 This way when this line of code was executing: 当这行代码执行时,这种方式是:

    $(".accordion-content-container").not($(this).next()).slideUp('fast'); $(“。accordion-content-container”)。not($(this).next())。slideUp('fast');

I could traverse up the DOM to the closest parent which was .accordion (but is now .accordion-container). 我可以遍历DOM到最接近的父级,即.accordion(但现在是.accordion-container)。

  1. $(".accordion-content-container").not($(this).next()).slideUp('fast').parent().find('span').removeClass('fa-minus'); $(“。accordion-content-container”)。not($(this).next())。slideUp('fast')。parent()。find('span')。removeClass('fa-minus') ;

Finally, in the instance when 'accordion-content-container' was being closed, 'parent()' looks back to 'accordion-container', then finds the <span> and removes the class 'fa-minus'. 最终,在关闭“ accordion-content-container”的情况下,“ parent()”返回“ accordion-container”,然后找到<span>并删除类“ fa-minus”。

I hope that makes sense, as usual, every day I'm learning something new. 我希望像往常一样,每天我都在学习新知识。

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

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