简体   繁体   English

jQuery中的分号语法让我感到困惑

[英]Semicolon syntax in jquery has me baffled

In the fairly simple code: 在相当简单的代码中:

<script> 
    $(document).ready(function () {
        $('.showprice').click(function () {
            $(this).parent.children.hide();
        }); 
    })
 </script>

Jshints in Fiddle fusses about the first line, saying, missing ; Fiddle中的Jshints对第一行大惊小怪,说,失踪了; and fusses at the last line with unrecoverable error. 并在最后一行大惊小怪,出现无法恢复的错误。

Q1: What am I missing here. Q1:我在这里想念什么。 My suspicion is that it's like perl, and is complaining about a semicolon when there is something entirely different wrong. 我的怀疑是,它像perl,并且在出现完全不同的错误时抱怨分号。

Q2: Pointers or links to better methods for tracking down syntax errors. Q2:指针或链接到更好的方法来跟踪语法错误。


Based on the first reply below, I made the changes suggested, bring the code up to this: 根据下面的第一条答复,我提出了建议的更改,将代码修改为:

I edited the code to be: 我将代码编辑为:

1 < script > 
2     $(document).ready(function () {
3     $('.showprice').click(function () {
4         $(this).parent().children().hide();
5     }); 
6    });
7    < /script>

And now I get fusses about missing ; 现在我为失踪烦恼; on line 1, "expected assignment or function call on line 6 在第1行上,“在第6行上有预期的赋值或函数调用
and unrecoverable error on line 7. 和第7行出现不可恢复的错误。

Firstly, you should change: 首先,您应该更改:

$(this).parent.children.hide();

to: 至:

$(this).parent().children().hide();

parent() and children() is a method. parent()children()是一种方法。 You need () here. 您需要在这里()

You also need to add ; 您还需要添加; after closing }) of your DOM ready wrapper, final code should look like this: 在结束}) DOM包装器后,最终代码应如下所示:

$(document).ready(function () {
    $('.showprice').click(function () {
        $(this).parent().children().hide();
    }); 
}); // <-- Here

You forgot to put ; 你忘了放; on the last ) of the code: 在代码的最后)

$(document).ready(function () {
    $('.showprice').click(function () {
        $(this).parent.children.hide();
    }); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM