使用 jQuery 构建树形菜单,我希望它可以访问,因此我使用了 aria 属性。我想要做的是在单击/输入时将“aria-expanded”属性从 true 切换为 false。我试过这个,但它显然不正确:

$(this).closest('ul').find('> li.tree-parent').toggleAttr( 'aria-expanded', 'true false' );

最佳答案

可以使用 .attr() 手动编写切换逻辑

$(this).closest('ul').find('> li.tree-parent').attr('aria-expanded', function (i, attr) {
    return attr == 'true' ? 'false' : 'true'
});

关于jquery - 使用 jQuery 切换元素的属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26772014/

10-11 22:06
查看更多