【问题标题】:jQuery $function is not workingjQuery $函数不工作
【发布时间】:2014-10-17 14:06:33
【问题描述】:
<html>
      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      <meta content="utf-8" http-equiv="encoding">
      <script type="text/javascript">

      $(document).ready(function () {           
           alert("Test");
      });

      </script>

      <script type="text/javascript" src="js/libs/jquery-2.1.1.min.js"></script>
</html>

如果我在 jquery 脚本之后调用 $(document).ready(function (){ }); 函数。然后它正在工作。但在上述情况下,它无法正常工作,并且 Firefox 控制台正在调试

ReferenceError: $ is not defined 火狐版本 =31.0
请问有人可以帮助解决这个问题吗?

谢谢,

【问题讨论】:

  • 为什么要在加载jquery之前运行jquery?
  • 我读到了 $(document).ready(function (){ });将在加载 DOM 后调用。所以它应该工作。为什么不工作?

标签: javascript jquery html dom


【解决方案1】:

试试这个,你应该在函数运行之前包含 JQuery(jquery-2.1.1.min.js)。

<html>
      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      <meta content="utf-8" http-equiv="encoding">
      <script type="text/javascript" src="js/libs/jquery-2.1.1.min.js"></script>
      <script type="text/javascript">
       $(document).ready(function () {           
           alert("Test");
      });  
      </script>

</html>

【讨论】:

    【解决方案2】:

    浏览器将按照您定义语句的顺序逐行编译。因此,您需要在使用其功能之前包含您的 jquery 库。

    就是这样。

    【讨论】:

    • 我读到了 $(document).ready(function (){ });将在加载 DOM 后调用。所以它应该工作。为什么不工作?
    • @Manku 你知道$(document).ready( 是一个jquery 函数吗?因此,如果您在添加 jQuery 库之前指定它,浏览器将不知道它是什么并抛出错误。它在DOM 准备就绪后调用,但在此之前编译
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多