Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:获取';使用严格的';错误_Javascript_Jquery_Html - Fatal编程技术网

Javascript:获取';使用严格的';错误

Javascript:获取';使用严格的';错误,javascript,jquery,html,Javascript,Jquery,Html,我是Javascript新手。我试图向Chrome控制台吐出一些东西,但在括号中的JSLint中出现了以下错误: '$' 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

我是Javascript新手。我试图向Chrome控制台吐出一些东西,但在括号中的JSLint中出现了以下错误:

'$' 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>

我的地址管理器
你的帐户

总余额
交易
开采的区块 活动

非常感谢您的帮助。

这些看起来像是lint错误——如果是,您需要指定开发模式。一些常量全局变量(如console或window)将抛出此错误。别理它

我不确定为什么(“ready”上的
不起作用——这就是内部称为
.ready()
的原因。我正在努力找出原因。请参阅下面的代码工作示例

$(document).on("ready", function() {
应该是

$(document).ready(function() {

这只是解决linting问题的一种方法,但您应该将JS放入一个立即调用的
函数中,并声明该函数的作用域为使用
“使用严格”

另外,将jQuery作为
IIF
的依赖项传入,并使用jslint指令告诉jslint您的期望值。在我们的例子中,我们说我们使用的是浏览器
browser:true
,这意味着
document
和许多其他特定于浏览器的全局变量将传递定义的linting。
devel:true
意味着hat
控制台
也应视为已定义

如果要在此源代码之前包含jQuery,那么您有一个外部全局函数,
jQuery
,因此您需要告诉jslint也需要这样做

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

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

您使用的是JSLinter吗?这些看起来像是lint错误——如果是,您需要指定开发模式。一些常量全局变量(如console或window)会引发此错误。忽略它。听起来好像您没有加载jQuery…@Meeseek我已经添加了jQuery脚本,html和js都在同一个文件中。您必须配置JSLint:Aspect:Brown将
$
控制台添加到全局变量列表中。JSLint还认为,“use strict”声明的正确位置在函数中。这可能是由于某些不受支持的编码字符,可能是IDE添加的,也可能是您错误地添加的。嗯……从技术上讲,
$(文档)。on(“ready”,function(){
没有错,只是因为几个原因它不是一个好的做法(除非它在最近的版本中被删除了,我只是不知道)@KevinB我想知道为什么
。on(“ready”
当时没有触发。我认为它也是对的(我应该重写我的声明…),但console语句没有与原始语句一起运行。可能还有一个我不知道的更改。它也没有与ready一起运行。我的jQuery标记在html中是否正确?它不会运行的唯一情况是,如果事件是在它已经触发后定义的,这里不应该是这种情况。确实很奇怪。如果jQuery脚本包含错了,你也会看到一个错误。