【问题标题】:uncaught reference $ is not defined未捕获的引用 $ 未定义
【发布时间】:2018-01-28 16:36:27
【问题描述】:

我正在尝试使用 javascript 做一个项目,使用键上的事件在屏幕上显示字母,我遇到了这个错误。请帮忙

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>

<script src="script.js" charset="utf-8"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>

This is the script file that I'm using

script.js

(document).keydown(function(event) {
  if (event.which === 13) {
    console.log("you pressed");
  }
});

【问题讨论】:

  • 您的页面中缺少 jquery 源代码,因此浏览器看不到 $ 字符。
  • 您是否忘记了您在此处发布的“脚本文件”中的$?你的意思是$(document).keydown(function(event) {

标签: javascript jquery html function keyboard


【解决方案1】:

你在 jQuery 之前运行你的脚本,所以 $ 还不存在。

【讨论】:

    【解决方案2】:

    地点

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    

    之前

    <script src="script.js" charset="utf-8"></script>
    

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2021-11-01
      • 2017-11-22
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多