【发布时间】:2017-11-05 19:18:11
【问题描述】:
我正在尝试使用 javascript 隐藏一堆表格行。 我想我的大部分代码都是正确的,我只是不知道为什么它没有运行隐藏。
感谢任何帮助。
代码是:
<script type="text/javascript">
function hideButton()
{
$("#showHideButton").on("click", function (e) {
var button = e.target;
var node = $(button).parent().parent().next();
while (!node.hasClass("service-header")) {
if ($(node).is(":visible")) {
$(node).hide();
} else {
$(node).show();
}
node = node.next();
}
})}
</script>
HTML 是:
for (loopHere)
{
echo "<tr class='service-header'><th>InfoHere</th><th>" . InfoHere . "<button style='float:right' type='button' id='showHideButton' class='btn btn-default' onclick='hideButton()'><span class='dashicons dashicons-arrow-down-alt2'></span></button></th></tr>";
echo "<tr><td>More Stuff</td></tr>";
echo "<tr><td>More Stuff</td></tr>";
}
loop
我不知道为什么它不会隐藏点击时的行,我已经在一个包含多个行的表上进行了测试,这些行具有类 service-header,但没有任何运气。
【问题讨论】:
-
e未在事件处理程序hideButton()中定义。您还将在每次单击元素时附加一个新的click事件。while循环的目的是什么? -
也发布 HTML ..也可以定义点击函数或在事件处理程序上使用,但不能同时使用
-
你也可以html标记
-
@guest271314 这是一个表,其中包含某些部分的标题,然后是该部分下的信息,然后是另一个标题,所以我希望能够隐藏没有“服务标头”类。该表是自动构建的,所以我有多个按钮需要隐藏它们下面的信息。你是说我只需要将 hideButton() 更改为 hideButton(e) 还是我还需要更改 onclick='hideButton()' 部分中的 html?感谢您的快速回复。
-
您可以在问题中包含
html吗?见stackoverflow.com/help/mcve
标签: javascript php jquery html html-table