[英]Javascript to set aria-expanded to false on <li> if other <li> is set to aria-expanded=true
This is my javascript code: 这是我的JavaScript代码:
<script type="text/javascript">
$(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');
}
});
});
</script>
I want my other li to set to aria-expanded=false if i click an li 如果我单击一个li,我希望其他li设置为aria-expanded = false
You can use jQuery's siblings() function to get the other li
elements: 您可以使用jQuery的siblings()函数来获取其他
li
元素:
if (menuItem.attr('aria-expanded') === 'true') {
$(this).attr('aria-expanded', 'false');
} else {
$(this).attr('aria-expanded', 'true');
$(this).siblings('li').attr('aria-expanded', 'false');
}
You can set it first to false for all <li>
elements and then enable it for the one you clicked on: 您可以先为所有
<li>
元素将其设置为false,然后为您单击的元素启用它:
<script type="text/javascript">
$(function () {
$('li').on('click', function (e) {
var menuItem = $(e.currentTarget);
// set it to false for every element
menuItem.parent().children('li').attr('aria-expanded', 'false');
// set it to true for the element you clicked on
menuItem.attr('aria-expanded', 'true');
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.