简体   繁体   English

当aria为true或false时,如何将addClass和removeClass添加到子元素?

[英]How to addClass and removeClass to child element when aria true or false?

I'm using the dropdown button by Zurb Foundation 5 with font awesome and the idea is to rotate the font when the menu is visible, and rotate it back once its hidden. 我使用的是Zurb Foundation 5下拉按钮,其字体很棒,其想法是在菜单可见时旋转字体,并在隐藏菜单后旋转回字体。

Zurb foundation 5 does not use class open on the element so hasClass doesn't work, instead it uses Aria and since hasAria doesn't exist I've tried to use some solutions that sadly don't work in one way or more. Zurb Foundation 5不使用在元素上打开的类,因此hasClass不起作用,而是使用Aria,并且因为hasAria不存在,所以我尝试使用一些无法以一种或多种方式起作用的解决方案。 What I'm trying to do is add the class fa-rotate-90 to the font element <i> when the parent .wf-burger is aria-expanded = true and remove it when aria-expanded = false . 我想做的是,当父.wf-burgeraria-expanded = true ,将class fa-rotate-90到字体元素<i> ,并在aria-expanded = false时将其删除。

Here is my rather simple HTML code: 这是我相当简单的HTML代码:

<h1>
    <a aria-expanded="false" aria-controls="drop1" data-dropdown="drop1" href="#" class="wf-burger">
        <i aria-hidden="true" class="fa fa-bars fa-rotate-90"></i>
    </a>
</h1> 
<ul aria-hidden="true" class="f-dropdown" id="drop1" data-dropdown-content>
    <li><a title="Link 1" href="#1">Link 1</a></li>
    <li><a title="Link 2" href="#2">Link 2</a></li>
    <li><a title="Link 3" href="#3">Link 3</a></li>
    <li><a title="Link 4" href="#4">Link 4</a></li>
    <li><a title="Link 5" href="#5">Link 5</a></li>
</ul>

This code works but doesn't remove the class once the aria is false: 这段代码有效,但是一旦aria为假,就不会删除该类:

(function($) {
    var attr = $('.wf-burger').attr('aria-expanded');
    if (typeof attr !== typeof undefined && attr !== false) {
            $('.fa.fa-bars').addClass('fa-rotate-90');
    }
    if (typeof attr !== typeof undefined && attr !== true) {
        $('.fa.fa-bars').removeClass('fa-rotate-90');
    }
})(jQuery);

This code didn't work at all: 这段代码根本不起作用:

(function($) {
    if ($('.wf-burger').attr('aria-expanded') === 'true') {
        $('.fa.fa-bars').addClass('fa-rotate-90');
    }
    if ($('.wf-burger').attr('aria-expanded') === 'false') {
        $('.fa.fa-bars').removeClass('fa-rotate-90');
    }
})(jQuery);

You can use this. 您可以使用它。 You just need to use $('.wf-burger').attr('aria-expanded') == 'true' . 您只需要使用$('.wf-burger').attr('aria-expanded') == 'true' No need to use the type check === . 无需使用类型检查===

Also using toggleClass() will be a good way for this. 同样,使用toggleClass()将是一个很好的方法。

 $('.fa.fa-bars').toggleClass( 'fa-rotate-90', $('.wf-burger').attr('aria-expanded') == 'true' ); 
 .fa-rotate-90 { border:1px solid red; height:20px; width:20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a aria-expanded="true" aria-controls="drop1" data-dropdown="drop1" href="#" class="wf-burger"> <i aria-hidden="true" class="fa fa-bars fa-rotate-90"></i> </a> <ul aria-hidden="true" class="f-dropdown" id="drop1" data-dropdown-content> <li><a title="Link 1" href="#1">Link 1</a> </li> <li><a title="Link 2" href="#2">Link 2</a> </li> <li><a title="Link 3" href="#3">Link 3</a> </li> <li><a title="Link 4" href="#4">Link 4</a> </li> <li><a title="Link 5" href="#5">Link 5</a> </li> </ul> 

A more simpler solution would be to simply toggle the class on click. 一个更简单的解决方案是只需单击单击即可切换类。

$(".wf-burger").click(function(){
   $('.wf-burger .fa').toggleClass('fa-rotate-90');
});

CSS solution CSS解决方案

I have just checked that it uses the open class when dropdown is opened, it is not working on docs, but it is working on the CodePen example, where I used the foundation 5.5.3 version. 我刚刚检查了打开下拉列表时它是否使用open类,它不适用于docs,但是它适用于CodePen示例,其中使用了Foundation 5.5.3版本。 http://cdn.foundation5.zurb.com/foundation.js http://cdn.foundation5.zurb.com/foundation.js

.wf-burger.open .fa{
  transform: rotate(90deg);
}

see example: http://codepen.io/shoaibik/pen/xVevrv 参见示例: http//codepen.io/shoaibik/pen/xVevrv

Updated Answer: 更新的答案:

The issue was that the dropdown should be sibling of the anchor which is attached to the dropdown, if you remove the H1 tag it should work. 问题是下拉列表应该是附加到下拉列表的锚的同级元素,如果删除H1标签,它应该可以正常工作。

<a aria-expanded="false" aria-controls="drop1" data-dropdown="drop1" href="#" class="wf-burger">
        <i aria-hidden="true" class="fa fa-bars fa-rotate-90"></i>
</a>
<ul aria-hidden="true" class="f-dropdown" id="drop1" data-dropdown-content>
    <li><a title="Link 1" href="#1">Link 1</a></li>
    <li><a title="Link 2" href="#2">Link 2</a></li>
    <li><a title="Link 3" href="#3">Link 3</a></li>
    <li><a title="Link 4" href="#4">Link 4</a></li>
    <li><a title="Link 5" href="#5">Link 5</a></li>
</ul>

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

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