当其父元素包含类active
时,我希望将子元素设置为aria-expanded="true"
<li class="dropdown active">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="true">
Section 4
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class=""><a href="#section41">Section 4-1</a></li>
<li class=""><a href="#section42">Section 4-2</a></li>
</ul>
</li>
这段代码来自一个示例here
当在该示例中,我向下滚动到4-1和4-2部分时,我希望下拉菜单打开。我已经尝试了几种方法,但还没有找到一个可行的解决方案。
发布于 2017-02-08 05:11:57
你真正想要的是将打开的类添加到活动的下拉列表中,你想要的效果与aria-expanded=true无关:
使用以下代码:
$(window).scroll(function(){
if ( $( ".dropdown" ).is( ".active" ) ) {
$( ".dropdown.active" ).addClass("open");
}
else
{
$( ".dropdown" ).removeClass("open");
}
});
发布于 2017-02-08 04:55:13
如果它发生在单击事件上,则使用类似下面的内容。
$('body').on('click', 'li.dropdown', function(){
if($(this).hasClass('active')){
$(this).children().attr('aria-expanded', true);
}
});
或
$('body').on('click', 'li.dropdown', function(){
if($(this).hasClass('active')){
$(this).find('a.dropdown-toggle').attr('aria-expanded', true);
}
});
https://stackoverflow.com/questions/42099247
复制相似问题