问题描述
我在我的 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 (将#修改为@)