简体   繁体   English

雪佛龙图标根据手风琴位置向上或向下指向

[英]Chevron Icon pointing up or down depending on accordion position

I've got an accordion I've built using jQuery UI. 我有一个手风琴我用jQuery UI构建。 I need the chevron icons that point up or down depending on if the section is open or closed. 我需要指向上或下的V形图标,具体取决于该部分是打开还是关闭。 The problem is my jQuery. 问题是我的jQuery。 At least initially it is. 至少最初它是。 I'm seeing both chevrons on load and once clicked the chevron doesn't change at all. 我看到两个V形臂都在加载,一旦点击,V形臂根本没有变化。

Jquery jQuery的

$(function() {

     $(".section a").click(function() {

          $(".chevron").removeClass("chevron").addClass("up");
    });
});

CSS CSS

.chevron {
background: url("images/down.png") no-repeat;
}

.up {
background: url("images/up.png");
}

HTML HTML

 <div class="section">
   <a href="#"><div class="tab active">
   <span class="chevron"></span><h3>Section 1</h3>
   </div></a> <!-- tab -->

You need to reference this so it toggles the element you are clicking on (not all of them at once). 您需要引用它,以便切换您单击的元素(不是一次全部)。 Once you have $(this) , you can use .find to search for the chevron within the link. 一旦你有了$(this) ,就可以使用.find来搜索链接中的V形符号

Finally, you can use toggleClass to switch between the class states. 最后,您可以使用toggleClass在类状态之间切换。 This allows you to click the link repeatedly and have it switch between the class states. 这允许您重复单击链接并使其在类状态之间切换。

$(function() {
    //Add down to all .chevrons
    $(".section a .chevron").addClass('down'); 

    //Toggle up/down classes
    $(".section a").click(function() {
        var $chevron = $(this).find('.chevron');                
        $chevron.toggleClass("down up");
    });
});

Then for CSS, set the classes to the correct chevron image: 然后对于CSS,将类设置为正确的V形图像:

.chevron.up { background-image('images/up.png'); }
.chevron.down { background-image('images/down.png'); }

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

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