简体   繁体   English

无法将类切换为元素

[英]Can't toggle class to element

I got problem with my accordion: I want when the panel opens jQuery adds a class expanded to the target(element) which I clicked and when the other panel opens, it removes that class and add it to the other one which opens. 我的手风琴有问题:我想在面板打开时jQuery向我单击的target(element)添加一个expanded的类,而在另一个面板打开时,它删除该类并将其添加到另一个打开的类中。
The default bootstrap functionality when I open the accordion panel it removes collapsed class of the a tag and when I close it, it adds it( collapsed class) again, I used this to solve my problem(which I explained it in paragraph above) but I couldn't. 默认的引导功能,当我打开手风琴面板它消除collapsed类的a标签,当我关闭它,它增加了它( collapsed类)再次,我用这个来解决我的问题(我解释过段以上),但我不能
here is the HTML code. 这是HTML代码。

<div class="panel-group" id="accordion">
     <div class="panel" >
          <div class="panel-heading">
               <a data-toggle="collapse" data-parent="#accordion" href="#html">HTML</a>
           </div>
            <ul id="html" class="panel-collapse collapse">
                 <li><a class="a_nav" href="#">Tags</a></li>
             </ul>
       </div>

       <div class="panel">
           <div class="panel-heading">
               <a data-toggle="collapse" data-parent="#accordion" href="#css">CSS</a>
           </div>
           <ul id="css" class="panel-collapse collapse">
               <li><a class="a_nav" href="#">Bootstrap</a></li>
           </ul>
       </div>
 </div>

and here is the jQuery code 这是jQuery代码

$(document).ready(function(){
      //add class to panel heading of sidebar for expanding
      $("div.panel-heading").click(function (event) {
              $target = $(event.target);
              $target.addClass("expanded");
              if($("div.panel-heading a").hasClass("collapsed")) {
                    $(this).removeClass("expanded");
                     //this line is for testing if the if condition even works
               $(this).text("ok");
            }
        });
});

I think this should solve your issue: 我认为这应该可以解决您的问题:

$('.panel-heading').click(function (event) {
  $(this).toggleClass('expanded');
  $('.panel-heading a').toggleClass('collapsed');
});

See the docs for toggleClass() . 请参阅docs关于toggleClass()

You are trying to get the event target and apply or remove the class to the event target every time. 您正在尝试获取事件目标,并且每次都将类应用于事件目标或将其删除。

As some users have said before me, maybe you should let bootstrap handle the accordion functionality. 正如一些用户在我之前说过的那样,也许您应该让引导程序处理手风琴功能。

But I will give a quick try to answer your problem. 但是,我将快速尝试回答您的问题。

You are using: 您正在使用:

$("div.panel-heading").click(function (event) { ... });

so you want to execute a function when an div with class panel-heading is clicked. 因此,当单击具有类面板标题的div时,您想执行一个函数。

Inside the function you do: 在函数内部,您可以执行以下操作:

$target = $(event.target);

To get the target element where the click event was fired. 获取触发点击事件的目标元素。

Now looking at your HTML I can see that .panel-heading divs contain a link as a child. 现在查看您的HTML,我可以看到.panel-heading div包含一个作为子链接的链接。

So event.target, will be the link itself if you click on the link or the .panel-heading div if you click on the div, so depending on where you happen to click, the class is going to be added to/removed from different elements. 因此,如果您单击该链接,event.target将是链接本身;如果您单击div,则将是.panel-heading div。因此,根据您碰巧单击的位置,该类将被添加到/从中删除。不同的元素。

This is something to watch. 这是要注意的事情。 Maybe instead of event.target, you can use $(this) in your event handling function. 也许可以在事件处理函数中使用$(this)代替event.target。

Below that, your logic is wrong because let's assume that you addClass("expanded") to the correct element, then this: 在此之下,您的逻辑是错误的,因为假设您将addClass(“ expanded”)添加到正确的元素,然后这样做:

$("div.panel-heading a").hasClass("collapsed");

Will look at a collection of elements so even if the first link doesn't have the class, the second link may have it, in which case the if statement will evaluate to true and the class you added before, will be removed from the element on which the event handler is called on. 将查看元素的集合,因此即使第一个链接没有该类,第二个链接也可能具有该类,在这种情况下,if语句的值将为true,并且您之前添加的类将从该元素中删除在其上调用事件处理程序。

To fix the issue, I would propose something like (if you don't want to let bootstrap do the work): 要解决此问题,我会提出类似的建议(如果您不想让引导程序来做):

$(".panel-heading").click(function () {

    var $target = $(this);

   //close all panels
    $(".panel-heading").removeClass("expanded");
    $(".panel-heading a").addClass("collapsed");

    //open clicked one
    $target.addClass("expanded");
    $("a", $target).removeClass("collapsed");

});

For better performance, I would cache the collections of elements used in the event handler in variables so that they don't get recalculated every time a click happens. 为了获得更好的性能,我将在事件处理程序中使用的元素集合缓存在变量中,以免每次单击时都不会重新计算它们。

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

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