【问题标题】:jquery code produces not defined errorjquery 代码产生未定义的错误
【发布时间】:2009-08-09 03:11:18
【问题描述】:

我不明白为什么下面的代码在 firebug 中给我这个错误

$未定义

谁能解释一下?

<script type="text/javascript" >
 $(document).ready(function(){
  setTimeout2(function(){
  $("#errormsg4").fadeOut("slow", function () {
  $("#errormsg4").remove();
      }); }, 5000);
 });
</script>

<div id="errormsg4">test</div>

【问题讨论】:

  • 你在哪里包含 jQuery 库?
  • 如果你将 jQuery 与另一个框架(例如 Prototype)一起使用,你必须包含 jQuery,包含 otherFramework,并且在你的内联脚本中,以:jQuery.noConflict(); 开头。跨度>
  • 我收到错误setTimeout2 is not defined
  • jquery 包含在脚本上方的页面中
  • @jason:我们能看到吗?它显然没有加载它:)

标签: jquery


【解决方案1】:

正如您所说的 $ 未定义,这意味着您在使用脚本代码之前没有给出 jquery 脚本文件的引用。另外我认为你打错了 setTimeout2,应该是 setTimeout。试试下面的代码

<html>
<head>
    <title></title>    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>    
</head>
<body>
    <div id="errormsg4">
        test</div>
</body>    
<script type="text/javascript">

    $(document).ready(function()
    {
        setTimeout(function()
        {
            $("#errormsg4").fadeOut("slow", function()
            {
                $("#errormsg4").remove();
            });
        }, 5000);
    });
</script>

</html>

【讨论】:

  • 好的,谢谢那是我的问题,setTimeout2 的命名我没有意识到 setTimeout 是 jquery 的预定义函数,我从其他地方复制了这段代码,我花了一个晚上阅读 jquery现在感觉更好
  • @jasondavis - setTimeout 不是 jQuery,它是 JavaScript。 jQuery 基本上是一堆 JavaScript 函数。请阅读更多内容:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多