使用匿名函数时的预期标识符

Expected Identifier When Using Anonymous Function

我有一个结构如下的 GridView:

虽然我有 Javascript 切换 dept-header 和 sub-elements 按预期工作,但切换在组 header 级别不起作用,因为其中一个的子元素dept-header 可以切换,但不能切换另一个 dept-header 所以当切换在 group-header 级别激活时它只是反转切换这不是我想要的所以我试着写一些 Javascript 当 group-header 被点击时,它只会隐藏所有并发行,而不管切换状态如何,直到下一个 group-header 如果它们没有被隐藏,否则点击 group-header 会 show/expand 隐藏行,无论切换状态如何,直到下一个 group-header.

由于是 Javascript 的新手,我目前 运行 在 anon 函数末尾左侧的 paren 处 'Expected identifier',无法确定原因看起来我有闭包,但我怀疑我对 anon func 使用了不正确的语法,而且我的函数可能写错了:

<script type="text/javascript">
    $(function () {
        $('.group-header').click(function () {
            $(this).nextUntil('.group-header').(function() {
                var el = document.getElementById(this);
                if (el.style.display != 'none') {
                    el.style.display = 'none';
                }
                else {
                    el.style.display = '';
                }
            })
        });
    });

    $(function () {
        $('.dept-header').click(function () {
            $(this).nextUntil('.dept-header, .group-header').toggle();
        });
    });
</script>

如何解决我遇到的错误?

错误是您的代码中有一个随机 ()。以为你错过了 each

$(this).nextUntil('.group-header').(function() {
                                 ^^^^

下一个问题是您不能将 this 与 getElementById 一起使用。

var el = document.getElementById(this);

应该只是

var el = this;