【问题标题】:Toggle Class Using Aria-Expanded使用 Aria-Expanded 切换类
【发布时间】:2016-05-25 18:09:55
【问题描述】:

我有以下方法可以根据父级的 aria-expanded attr 值切换 span 元素的类:

$(function () {
    if ($('.is-accordion-submenu-parent').attr('aria-expanded') === "true") {
        $(this).find(".typcn-arrow-sorted-down").toggleClass("typcn-arrow-sorted-up");
    }
})

结构如下:

<li class="is-accordion-submenu-parent" aria-expanded="false">
  <a>Label</a>
  <span class="typcn typcn-arrow-sorted-down"></span>
</li>

编辑

  <li class="is-accordion-submenu-parent" aria-expanded="false">
    <a>Label<span class="typcn typcn-arrow-sorted-down"></span></a>
    <ul class=“nested”>
      <li class=“menu item”>…</li>
      …etc…
    </ul>
  </li>

单击 a 标签会切换状态,但此代码无法按预期工作。有任何想法吗?

【问题讨论】:

    标签: javascript jquery toggleclass


    【解决方案1】:

    切换typcn-arrow-sorted-up 是不够的,因为您可能还需要切换typcn-arrow-sorted-down 这个类。 这是一个工作示例,显示了基于 aria 属性的两个分类之间的切换。我还添加了一个按钮来更改属性值以检查它是否有效。

    $(function () {
      $('#ariaToggler').click(function(){
        var currValue = $('.is-accordion-submenu-parent').attr('aria-expanded');
        if(currValue == 'true'){
          currValue = 'false';
        }else{
          currValue = 'true';
        }
        $('.is-accordion-submenu-parent').attr('aria-expanded', currValue);
        checkup();
      })
      function checkup(){
        $(".typcn").removeClass('typcn-arrow-sorted-up typcn-arrow-sorted-down');
        if ($('.is-accordion-submenu-parent').attr('aria-expanded') === "true") {
            
            $(".typcn").addClass("typcn-arrow-sorted-up");
        }else{
          $('.typcn').addClass('typcn-arrow-sorted-down');
        }
      }
      checkup();
    })
    .typcn{
    display:block;
      height:50px;
      width:100px;
    
    }
    
    .typcn-arrow-sorted-down{
      
    background-color:red;
    }
    .typcn-arrow-sorted-up{
    background-color:green;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="ariaToggler">toggle Attr</button>
    
    <li class="is-accordion-submenu-parent" aria-expanded="false">
      <a>Label</a>
      <span class="typcn typcn-arrow-sorted-down"></span>
    </li>
    $(".typcn").removeClass('typcn-arrow-sorted-up typcn-arrow-sorted-down');
    if ($('.is-accordion-submenu-parent').attr('aria-expanded') === "true") {
      $(".typcn").addClass("typcn-arrow-sorted-up");
    }else{
      $('.typcn').addClass('typcn-arrow-sorted-down');
    }
    

    编辑

    添加了另一个示例,该示例使用 toggleClass 并使用相对于单击标签的选择器,因此这甚至适用于多个。

    $(function () {
      $('.is-accordion-submenu-parent a').click(function(){
        var currValue = $(this).parent().attr('aria-expanded');
        if(currValue == 'true'){
          currValue = 'false';
        }else{
          currValue = 'true';
        }
        $(this).parent().attr('aria-expanded', currValue);
        $(this).next().toggleClass('typcn-arrow-sorted-down');
        $(this).next().toggleClass('typcn-arrow-sorted-up');
    
      })
    

    $(function () {
      $('.is-accordion-submenu-parent a').click(function(){
        var currValue = $(this).parent().attr('aria-expanded');
        if(currValue == 'true'){
          currValue = 'false';
        }else{
          currValue = 'true';
        }
        $(this).parent().attr('aria-expanded', currValue);
        $(this).next().toggleClass('typcn-arrow-sorted-down');
        $(this).next().toggleClass('typcn-arrow-sorted-up');
        
      })
    
    })
    .typcn{
        display:inline-block;
          height:15px;
          width:30px;
    
        }
    
        .typcn-arrow-sorted-down{
          
        background-color:red;
        }
        .typcn-arrow-sorted-up{
        background-color:green;
        }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <li class="is-accordion-submenu-parent" aria-expanded="false">
      <a>Label 1</a>
      <span class="typcn typcn-arrow-sorted-down"></span>
    </li>
    <li class="is-accordion-submenu-parent" aria-expanded="false">
      <a>Label 2</a>
      <span class="typcn typcn-arrow-sorted-down"></span>
    </li>
    <li class="is-accordion-submenu-parent" aria-expanded="false">
      <a>Label 3</a>
      <span class="typcn typcn-arrow-sorted-down"></span>
    </li>

    【讨论】:

    • 这是有道理的,但尽管您的测试成功,但它并没有按预期工作。添加了向下箭头类,但未在 aria-expanded=true 状态下切换。
    • @WesleyPicotte 我又添加了一个样本,我相信这是您的要求。如果你觉得,它不是你需要的。请考虑更详细地解释它
    • 感谢您的更新。我已经编辑了这个 sn-p 目标的 HTML,以更好地展示完整的结构。修改后的 sn-p 将两个 typcn 类添加到 ul.nested。
    • @WesleyPicotte 你能添加解释应该做什么的文本吗?截至现在单击我认为是锚标签的标签,您需要切换aria-expanded 和跨度中的类,该类正在编辑中工作。嵌套的ul 不会在问题陈述中添加任何内容
    • 正确 - 单击 a 元素可切换菜单。菜单的插件以 ul.nested 为目标,它显然保存了手风琴的内容。您的 sn-p 在我看来是正确的,但无论出于何种原因,都会遍历到 ul,这是我看到两个类都添加的地方。我不确定我可以提供哪些额外信息。
    【解决方案2】:

    通过定位 *-expanded 属性并在伪元素之后使用 CSS 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2021-12-26
      • 2018-10-20
      • 1970-01-01
      • 2021-02-11
      • 2017-02-17
      相关资源
      最近更新 更多