【发布时间】:2021-01-15 20:09:53
【问题描述】:
我有一堆这样的 HTML 设置:
<button class="btns">Button A</button>
<ul class="unorder-list" aria-hidden="true">
<li>List</li>
<li>List</li>
</ul>
<button class="btns">Button B</button>
<ul class="unorder-list" aria-hidden="true">
<li>List</li>
<li>List</li>
</ul>
所以所有的按钮都是可见的,但所有的<ul> 都是隐藏的。单击按钮时,我希望它切换相邻的<ul> 以显示并再次单击以隐藏。
我有一些类似这样的代码,以便为按钮单击事件设置事件侦听器,但需要执行上述操作并将 aria-hidden 属性调整为 false。
var e, divs = document.getElementsByClassName('btns');
var index = 0;
var ul = document.getElementsByClassName('unorder-list');
// var list
for (e in divs) {
var i = e;
if (divs.hasOwnProperty(e)) {
divs[e].onclick = function() {
// Returns 'namedItem'
alert(e);
// Returns undefined
alert(ul[index]);
// Error
var visible = ul[index].style.display;
// Do work here to show/hide and adjust attributes as needed
alert(visibile);
// document.getElementById(id).style.display = 'block';
};
index++;
}
}
我写这篇文章是为了确保代码格式正确且易于访问。
【问题讨论】:
标签: javascript html ecmascript-6