为什么这段代码会抛出

未捕获引用错误:未定义$

以前什么时候还好?

$(document).ready(function() {
    $('#tabs > ul').tabs({ fx: { opacity: 'toggle' } });
    $('#featuredvid > ul').tabs();
});

选项卡中的结果不再关闭。

jQuery在头中被引用:

<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/sprinkle.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/jquery-1.2.6.min.js"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo get_option('siteurl') ?>/js/jquery-ui-personalized-1.5.2.packed.js"></script>

当前回答

如果是wordpress,可能需要更改

$(document).ready(function() {

to

jQuery(document).ready(function($){

或添加

var $ = jQuery;

之前

$(document).ready(function() {

其他回答

我也有类似的问题。我的测试服务器使用“http”时工作正常。然而,它在具有SSL的生产中失败。

因此,在生产中,我添加了“HTPPS”而不是“HTTP”,在测试中,我保持了“HTTP”。

测试:

wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', array(), null, false );

wp_register_script( 'custom-script', plugins_url( '/js/custom.js', __FILE__ ), array( 'jquery' ) );

生产:

wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', array(), null, false );

wp_register_script( 'custom-script', plugins_url( '/js/custom.js', __FILE__ ), array( 'jquery' ) );

希望这对正在wordpress工作的人有所帮助。

根本问题是ReferenceError。MDN表示try/catch块是作业的正确工具。在我的例子中,我得到了一个付费sdk/库的未经修正的参考错误。以下内容对我有用。

try {
  var stripe = Stripe('pk_test_---------');
} catch (e) {
    stripe = null;
}

if(stripe){
  //we are good to go now
}

显然,修复方法是在调用其方法之前加载任何SDK/库,例如jQuery,但try/catch确实可以防止共享JavaScript弹出错误,以防您在页面上运行共享脚本,而该页面没有加载您在页面子集上使用的任何库。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="local_xxx.js"></script>

我也有类似的问题,你知道吗,这是因为当我在android设备webview中测试时,网络断开了,我没有意识到这一点,而本地js加载没有问题,因为它不需要网络。这看起来很可笑,但我花了1个小时才弄清楚。

在包含jQueryJavaScript之前,您正在调用ready()函数。首先引用jQuery。

如果您使用的是ASP.NET MVC,则可以通过执行以下操作指定何时呈现JS代码:

1,在page.cshtml中,将<script>标记包装到一个部分中,并为其命名,常用名称为“script”:

@section scripts {
    <script>
        // JS code...
    </script>
}

2、在_Layout.cshtml页面中,添加@RenderSection(“Scripts”,必需:false),确保在引用Jquery源代码后放置它,这将使JS代码呈现晚于Jquery。

我在尝试运行不同的web套接字示例时遇到了这个问题。

将浏览器指向“”时http://localhost:8080/'我收到了这个错误,但正好指向'http://localhost:8080/index.html'没有给出任何错误,因为在'index.html'中,所有都完全正常-jquery在使用$..之前已包含。。

不知为何,自动转发到index.html没有完全工作