jQuery根据其值更改aria-expanded属性

时间:2017-08-17 19:54:57

标签: jquery attr wai-aria

我有一个项目,我想根据当前状态更改aria扩展属性。

它将OK更改为true,但由于某些原因,在第二次单击时不会更改为false:

HTML

 <li aria-expanded=false>hey</li>

的jQuery

$("li").on("click", function(e) {
     var menuItem = $(e.currentTarget);

     if (menuItem.attr("aria-expanded") == true) {
          $(this).attr("aria-expanded", false);
     } else {
          $(this).attr("aria-expanded", true);
     }
     return false;
});

https://codepen.io/anon/pen/jLYRpg

1 个答案:

答案 0 :(得分:1)

HTML属性是字符串,因此您应该使用字符串版本truefalse

&#13;
&#13;
$(function () { 
    $('li').on('click', function (e) {
        var menuItem = $( e.currentTarget );

        if (menuItem.attr( 'aria-expanded') === 'true') {
            $(this).attr( 'aria-expanded', 'false');
        } else {
            $(this).attr( 'aria-expanded', 'true');
        }
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li aria-expanded="false">hey</li>
&#13;
&#13;
&#13;