2015-10-26 101 views
-2

我是JavaScript新手。我试图吐出一些东西到Chrome控制台,但我得到这些错误在JSLint的括号:Javascript:正在使用'严格'错误

'$' was used before it was defined. $(document).on("ready", function() { 
3 Expected exactly one space between 'function' and '('. $(document).on("ready", function() { 
4 Missing 'use strict' statement. console.log("Address Explorer JS up and running."); 
4 Expected 'console' at column 5, not column 3. console.log("Address Explorer JS up and running."); 
4 'console' was used before it was defined. console.log("Address Explorer JS up and running."); 

这里是JS代码:

"use strict"; 
/* address-explorer.js */ 

$(document).on("ready", function() { 
    console.log("Address Explorer JS up and running."); 
}); 

我尝试添加了“使用严格的”;声明在文件的顶部,但这些错误不会消失,也不会向控制台吐出。这里是HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <title>My Address Explorer</title> 
    <link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet"> 
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script> 
    <script type="text/javascript" src="./address-explorer.js"></script> 
</head> 
<body> 
    <div class="container"> 
     <h1 class="twelve coloumns"> 
      Your Account 
     </h1> 
     <div class="five coloumns"> 
      <span id="displayed_balance" class="value">&nbsp;</span> 
      <br><span class="label"> Total Balance</span> 
     </div> 
     <div class="four coloumns"> 
      <span id="transaction_count" class="value">&nbsp;</span> 
      <br><span class="label"> Transactions</span> 
     </div> 
     <div class="two coloumns"> 
      <span id="blocks_mined_count" class="value">&nbsp;</span> 
      <br><span class="label">Blocks Mined</span> 
     </div> 
     <h4 class="twelve columns">Activity</h4> 
    </div> 
</body> 
</html> 

帮助将不胜感激。

+0

您使用的是JSLinter吗?这些看起来像lint错误 - 如果是这样,你需要指定开发模式。一些像控制台或窗口这样的常量全局变量会抛出这个错误。忽略它。 –

+0

这听起来像你还没有加载jQuery ... – rnevius

+0

@Meeseeks我已经添加了jQuery脚本,并且html和js都在同一个文件中。 – Shashank

回答

2

这些看起来像lint错误 - 如果是这样,您需要指定开发模式。一些像控制台或窗口这样的常量全局变量会抛出这个错误。忽略它。

我不知道为什么on("ready"不起作用 - 这就是内部所谓的.ready()。我正在研究为什么。有关您的代码工作的示例,请参见此fiddle

$(document).on("ready", function() { 

应该

$(document).ready(function() { 
+0

好吧......从技术上讲,'$(document).on(“ready”,function(){'没有错,只是出于几个原因不是好的做法(除非它在最近的版本中被删除我只是不知道) –

+0

@KevinB我想知道为什么'.on(“ready”'没有触发,我认为它也是正确的(我应该重新说明我的声明..),但控制台声明没有' t运行与原来的。也许有一个变化,我不知道以及。 –

+0

https://gyazo.com/b3011d5e5529a899e4cb79df520a4e5f它没有运行就绪。我的jQuery标签正确的HTML? – Shashank

0

这仅仅是一个解决您的掉毛问题的方式,但你应该把你的JS到Immediately Invoked Function,并宣布其功能范围,使用"use strict";

而且,将jQuery作为您的IIF的依赖项并使用jslint指令告诉jslint您的期望。在我们的例子中,我们说我们使用浏览器browser:true,这意味着document和许多其他浏览器特定的全局变量将通过定义的线索。 devel: true意味着console也应被视为定义。

如果您在此源代码之前包含jQuery,则您有外部全局,jQuery,因此您需要告诉jslint也期望这一点。我们用/*global ...*/指令来做到这一点。

/*jslint browser:true, devel: true*/ 
/*global jQuery*/ 
(function ($) { 
    "use strict"; 

    $(document).on("ready", function() { 
     console.log("Address Explorer JS up and running."); 
    }); 
}(jQuery)); 
相关问题