Javascript 类型错误 $ 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12166883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:17:33  来源:igfitidea点击:

Type Error $ is not a function

javascriptjquery

提问by L84

I am using a jQuery Based Memory Gameon my site. It works well overall but I want a "Play Again" Link and stats at the end to show. I cannot get that code to work. I see this error:

我在我的网站上使用基于jQuery 的记忆游戏。它总体上运行良好,但我想要一个“再次播放”链接和最后显示的统计数据。我无法让该代码工作。我看到这个错误:

TypeError: $ is not a function
  [Break On This Error]     

    var game = $('div.slashc-memory-game'); // get the game

Here is the JS:

这是JS:

    // this script shows how you can get info about the game
    var game = $('div.slashc-memory-game'); // get the game
    var info = $('p#info').find('span'); // get the info box
    var playAgain = $('a#play-again').css('visibility', 'hidden'); // get the play again link
    // format time like hh:mm:ss
    var formatTime = function(s)
    {
        var h = parseInt(s / 3600), m = parseInt((s - h * 3600) / 60); s = s % 60;
        return (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
    }
    // listen for game 'done' event 
    game.bind('done', function(e)
    {
        // show basic stats
        var stats = game.slashcMemoryGame('getStats');
        info.html('Success ! Number of clicks : ' + stats.numClicks + ', elapsed time : ' + formatTime(parseInt(stats.time / 1000)) + '.');
        playAgain.css('visibility', 'visible'); // show link
    });
    // play again action
    playAgain.click(function(e)
    {
        playAgain.css('visibility', 'hidden'); // hide link
        info.html('Memory Game, click to reveal images'); // reset text
        game.slashcMemoryGame('restart'); // restart game
        e.preventDefault();
    });

Here is a Fiddle

这是一个小提琴

采纳答案by Richard Robertson

Try replacing $() with jQuery() and see if you continue to get this error. The error indicating that it isn't a function means that either the jQuery file was not loaded yet or the $ name was freed intentionally.

尝试用 jQuery() 替换 $() 并查看是否继续出现此错误。指示它不是函数的错误意味着尚未加载 jQuery 文件或故意释放 $ 名称。

If jQuery() function doesn't work, make sure that you are loading the jQuery file before your script block/file.

如果 jQuery() 函数不起作用,请确保在脚本块/文件之前加载 jQuery 文件。

回答by Pevara

as fiddle automaticly adds the onload function, your code was actually working fine: http://jsfiddle.net/ZXMUB/5/

由于小提琴会自动添加 onload 功能,您的代码实际上运行良好:http: //jsfiddle.net/ZXMUB/5/

All i did was uncomment the first line where the game variable is beeing assigned, and remove closing </script>tag from the js window.

我所做的只是取消注释分配游戏变量的第一行,并</script>从 js 窗口中删除结束标记。

Are you sure on your actual site the code is inside a $(document).ready function (or something similar) and the jQuery library gets loaded properly?

您确定在您的实际站点上,代码位于 $(document).ready 函数(或类似函数)中并且 jQuery 库已正确加载?

回答by Lucas Green

Your code was missing a document ready handler. Updated:

您的代码缺少文档就绪处理程序。更新:

http://jsfiddle.net/ZXMUB/1/

http://jsfiddle.net/ZXMUB/1/

回答by Chris Carew

wrap your code in a "ready" function, like so

将您的代码包装在“就绪”函数中,就像这样

$(function(){  

 ...

});

Your code is executing before jquery is loaded.

您的代码正在加载 jquery 之前执行。