jQuery - “$ symbol is undefined”

时间:2012-05-31 11:10:50

标签: javascript jquery

我在我的网站上成功使用了一些插件大约6个月,突然间我开始收到我的一个脚本的消息:“$ is undefined”。

这是有问题的代码行:$.widget("ui.ImageColorPicker", uiImageColorPicker);

我正在使用这个插件,几乎是最新的启用了noConflict的jQuery:http://github.com/Skarabaeus/ImageColorPicker我没有改变任何东西6个月(我没有更新jQuery)。我确信它在2周前工作正常,现在它突然崩溃了。

编辑:错误消失了。我正在删除示例网站。

5 个答案:

答案 0 :(得分:5)

它看起来像一个可变范围问题。

尝试将第一行更改为:

(function($){

以及最后一行:

})(jQuery);

通过执行此操作,我们将jQuery对象传递给围绕插件的匿名函数,但通过将其引用为$,块中的代码可以使用' normal& #39; jQuery,同时仍然在页面的其余部分保持noConflict模式。

答案 1 :(得分:1)

请试试这个

<script type="text/javascript" src="other_lib.js"></script> //In your case try Imagecolorpicker js
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  // Code that uses other library's $ can follow here.
</script>

Reference $ .noConflict();

答案 2 :(得分:0)

尝试使用明确的jQuery对象而不是$别名 例如:

jQuery.widget("ui.ImageColorPicker", uiImageColorPicker);

答案 3 :(得分:0)

看起来在该页面上定义了jQuery.Widget。试试而不是$.widget

答案 4 :(得分:0)

首先,确保包含jQuery UI。尝试使用jQuery.widget。如果这不起作用,请使用:

e = jQuery; // you can use anything else than $

现在,您应该输入e.widget("ui.ImageColorPicker", uiImageColorPicker); 而不是你拥有的。