如何解决表格中的简单手风琴 js 问题
我在我的 WP 站点中为表格插件添加了一个代码片段,该插件应该在表格中的每个产品行下添加一个手风琴,单击时将显示更多信息。
第一个手风琴正常工作,但是每个产品行下的所有其他手风琴在点击时都没有响应。
我该如何解决这个问题?
<script>
var i;
var acc = document.getElementsByClassName("accordion");
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click",function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "table-row") {
panel.style.display = "none";
} else {
panel.style.display = "table-row";
}
});
}
</script>
<tr class="accordion">
<td colspan="100">Find out more about
<?PHP global $product;
echo $product->get_short_description();
?>
</td>
</tr>
<tr class="panel">
<td colspan="100">
<?PHP global $product;
echo $product->get_description();
?>
</td>
</tr>
SNIPPET UPDATE(似乎工作见评论)
var i;
var acc = document.getElementsByClassName("accordion");
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click",function() {
/* this.classList.toggle("active") */;
var panel = this.nextElementSibling;
if (panel.style.display === "table-row") {
panel.style.display = "none";
} else {
panel.style.display = "table-row";
}
});
}
<table>
<tr class="accordion">
<td>Find out more about 1
</td>
</tr>
<tr class="panel">
<td>
Panel 1
</td>
</tr>
<tr class="accordion">
<td>Find out more about 2
</td>
</tr>
<tr class="panel">
<td>
Panel 2
</td>
</tr>
<tr class="accordion">
<td>Find out more about 3
</td>
</tr>
<tr class="panel">
<td>
Panel 3
</td>
</tr>
</table>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。