是否可以使用 JSLint 验证我的 jQuery JavaScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4071467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to validate my jQuery JavaScript with JSLint?
提问by newtogit
I wanted to check my JavaScript with JSLint. I am also using jQuery and JSLint seems to be very unhappy about jQuery.
我想用 JSLint 检查我的 JavaScript。我也在使用 jQuery,而 JSLint 似乎对 jQuery 非常不满意。
So, if I have this code:
所以,如果我有这个代码:
$(document).ready{
$("a").click(function() {
// foo
});
});
I am getting a few JSLint messages:
我收到一些 JSLint 消息:
Error:
Problem at line 1 character 1: '$' was used before it was defined.
$(document).ready{
...
错误:
第 1 行字符 1 的问题:在定义之前使用了“$”。
$(document).ready{
...
I had a look at the options but I couldn't figure out how to tell JSLint that this is ok.
我查看了选项,但我不知道如何告诉 JSLint 这没问题。
If I had only one jQuery call in my code I could just ignore the JSLint message but in 2,500 lines of script there are lots of calls, I can't find the errors I'd like to fix between all these messages.
如果我的代码中只有一个 jQuery 调用,我可以忽略 JSLint 消息,但是在 2,500 行脚本中有很多调用,我无法在所有这些消息之间找到我想要修复的错误。
So, does anyone know how to configure JSLint such that it works with jQuery calls? Or is there something else I can use to check the quality of my JavaScript/jQuery code?
那么,有谁知道如何配置 JSLint 使其与 jQuery 调用一起使用?或者还有什么我可以用来检查我的 JavaScript/jQuery 代码质量的东西吗?
回答by Nick Craver
That's not valid JavaScript, it should be:
这不是有效的 JavaScript,它应该是:
$(document).ready(function() {
So JSLint will (appropriately) complain about your syntax.
所以 JSLint 会(适当地)抱怨你的语法。
If you want to get rid of the Implied global: $, document
message, go to the bottom where it has Predefined (, separated), and put jQuery, $
in that textbox then run again. For the document
piece, check Assume a browserin the first column.
如果您想删除该Implied global: $, document
消息,请转到具有Predefined (, separator)的底部,然后放入jQuery, $
该文本框,然后再次运行。对于这document
件作品,请选中第一列中的假设使用浏览器。
All JSLint settings will stick, so you don't have to do this each time you go back.
所有 JSLint 设置都将保留,因此您不必每次返回时都这样做。
回答by Andy Balaam
Add this at the top of your document to get it to validate:
将此添加到您的文档顶部以使其验证:
/*jslint browser: true*/ /*global $*/
回答by Vincent
Nick is correct about your syntax. The latest release of jQuery (1.4.3) passes JSLint tests. See this blog post: http://blog.jquery.com/2010/10/16/jQuery-143-released/
尼克对你的语法是正确的。最新版本的 jQuery (1.4.3) 通过了 JSLint 测试。请参阅此博客文章:http: //blog.jquery.com/2010/10/16/jQuery-143-released/