未捕获的 ReferenceError:未定义 jQuery

IT技术 javascript jquery
2021-03-01 12:08:10

我已经在我的网站上实现了一些 JavaScript,但我不断收到以下错误消息:

未捕获的 ReferenceError:未定义 jQuery

未捕获的语法错误:意外的标记 <

这是我在 header.php 中使用的 JavaScript:

    <script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.jcarousel.min.js"></script>
    <script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.scrollTo.js"></script>
    <script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.backgroundPosition.js"></script>
    <script type="text/javascript" src="/test/wp-content/themes/child/script/scripts.js"></script>
    <script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.cycle.lite.js"></script>
    <script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.accordian.js"></script>

    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery('#contentGallery').cycle({
                fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            });
        });
    </script>
</head>

<body <?php body_class(); ?>>
<div id="page" class="hfeed">
    <header id="branding" role="banner">
            <hgroup>
                <h1 id="site-title"><span><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></span></h1>
                <h2 id="site-description"><?php bloginfo( 'description' ); ?></h2>
            </hgroup>

            <?php
                // Check to see if the header image has been removed
                $header_image = get_header_image();
                if ( ! empty( $header_image ) ) :
            ?>
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>">
                <?php
                    // The header image
                    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
                    if ( is_singular() &&
                            has_post_thumbnail( $post->ID ) &&
                            ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( HEADER_IMAGE_WIDTH, HEADER_IMAGE_WIDTH ) ) ) &&
                            $image[1] >= HEADER_IMAGE_WIDTH ) :
                        // Houston, we have a new header image!
                        echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
                    else : ?>
                    <img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
                <?php endif; // end check for featured image or standard header ?>
            </a>
            <?php endif; // end check for removed header image ?>

            <nav id="access" role="navigation">

<div id="nav">
            <ul>
                <li class="end"><a href="#contact" id="navContact" class="goto_contact"></a></li>
                <li><a href="#context" id="navContext" class="goto_context"></a></li>
                <li><a href="#artScience" id="navArtScience" class="goto_artScience"></a></li>
                <li><a href="#home" id="navHome" class="goto_home"></a></li>
            </ul>
            <div class="clear">
            </div>
        </div>
        <div id="navPointer">
            <div id="controlContainer">
                <div id="pointer">
                </div>
            </div>
        </div>
    </div>
    <div id="contentHolder">
        <div id="contentGallery">
            <img src="Images/Gallery/london.jpg" width="1200" height="550" alt="London" />
            <img src="Images/Gallery/singapore.jpg" style="display: none;" width="1200" height="550"
                alt="Singapore" />
            <img src="Images/Gallery/geneva.jpg" style="display: none;" width="1200" height="550"
                alt="Geneva" />
        </div>
        <div id="contentShadow">
        </div>
        <div id="content">
            <div id="contentScroller">
                <div id="home" class="page">
                    <div class="homeContent">
                        <span class="homeHeaderText">GMR</span>
                        <div class="clear">
                        </div>
                        <div class="homePageText">
                            <p> very long text 1
                            </p>
                        </div>
                    </div>
                </div>
                <div id="artScience" class="page">
                    <div class="pageContent">
                        <div id="artSciencePage">
                            <span class="headerText">About Us</span>
                            <div class="pageText">
                                <p>
                                    Insert text here. 
                                </p>
                            </div>
                            <table border="0" cellpadding="0" cellspacing="0" class="linkTable">
                                <tr>
                                    <td valign="middle" height="100%">
                                        <a href="#" id="theArtLnk" class="largeArrow">The Art</a>
                                    </td>
                                    <td valign="middle" height="100%">
                                        <a href="#" id="theScienceLnk" class="largeArrow">The Science</a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div id="theArtPage" class="closed">
                            <span class="headerText">The Art</span>
                            <div class="pageText">
                                <a href="#" class="acc_trigger">Sensitivity</a>
                                <div class="acc_container">
                                    <p>very long text 2</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger">Creativity</a>
                                <div class="acc_container">
                                    <p>very long text 3</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger">Intuition</a>
                                <div class="acc_container">
                                    <p>very long text 4</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger">Judgment</a>
                                <div class="acc_container">
                                    <p>very long text 5</p>
                                </div>
                            </div>
                            <a href="#" id="artToScienceLnk" class="largeArrow">The Science</a>
                        </div>
                        <div id="theSciencePage" class="closed">
                            <span class="headerText">The Science</span>
                            <div class="pageText">
                                <a href="#" class="acc_trigger_2">Methodology</a>
                                <div class="acc_container_2">
                                    <p>very long text 6</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger_2">Research</a>
                                <div class="acc_container_2">
                                    <p>very long text 7</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger_2">Team Approach</a>
                                <div class="acc_container_2">
                                    <p>very long text 8</p>
                                </div>
                                <div class="seperator">
                                </div>
                                <a href="#" class="acc_trigger_2">Sharing Information</a>
                                <div class="acc_container_2">
                                    <p>very long text 9</p>
                                </div>
                            </div>
                            <a href="#" id="scienceToArtLnk" class="largeArrow">The Art</a>
                        </div>
                    </div>
                </div>
                <div id="context" class="page">
                    <div class="pageContent">
                        <span class="headerText">Expertise</span>
                        <div class="pageText">
                            <a class="acc_trigger_3" href="#">Expertise</a>
                            <div class="acc_container">
                                <p>very long text 10</p>
                            </div>
                            <div class="seperator">
                            </div>
                            <a class="acc_trigger_3" href="#">Business Context</a>
                            <div class="acc_container">
                                <p>very long text 11</p>
                            </div>
                            <div class="seperator">
                            </div>
                            <a class="acc_trigger_3" href="#">Cultural Context</a>
                            <div class="acc_container">
                                <p>very long text 12</p>
                            </div>
                            <div class="seperator">
                            </div>
                            <a class="acc_trigger_3" href="#">Candidate Context</a>
                            <div class="acc_container">
                                <p>very long text 13</p>
                            </div>
                            <div class="seperator">
                            </div>
                            <a class="acc_trigger_3" href="#">Financial Context</a>
                            <div class="acc_container">
                                <p>very long text 14</p>
                            </div>
                            <div class="seperator">
                            </div>
                            <a class="acc_trigger_3" href="#">Service Context </a>
                            <div class="acc_container">
                                <p>very long text 15</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div id="contact" class="page">
                    <div class="pageContent">
                        <span class="headerText">Contact</span>
                        <div class="pageText">
                            <h2>
                                Tel: +44(0)1234 567 890</h2>
                            <h3>
                                Email: <a href="mailto:info@info">info@info</a></h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>

                <h3 class="assistive-text"><?php _e( 'Main menu', 'twentyeleven' ); ?></h3>
                <?php /*  Allow screen readers / text browsers to skip the navigation menu and get right to the good stuff. */ ?>
                <div class="skip-link"><a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to primary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to primary content', 'twentyeleven' ); ?></a></div>
                <div class="skip-link"><a class="assistive-text" href="#secondary" title="<?php esc_attr_e( 'Skip to secondary content', 'twentyeleven' ); ?>"><?php _e( 'Skip to secondary content', 'twentyeleven' ); ?></a></div>
                <?php /* Our navigation menu.  If one isn't filled out, wp_nav_menu falls back to wp_page_menu. The menu assiged to the primary position is the one used. If none is assigned, the menu with the lowest ID is used. */ ?>

            </nav><!-- #access -->
    </header><!-- #branding -->

这是我在 footer.php 中使用的代码:

<div id="footerNav">
    <ul>
        <li class="start"><a href="#home" class="goto_home">Home</a></li>
        <li><a href="#artScience" class="goto_artScience">About Us</a></li>
        <li><a href="#context" class="goto_context">Approach</a></li>
        <li><a href="#contact" class="goto_contact">Expertise</a></li>
    </ul>
</div>
5个回答

jQuery 需要是您导入第一个脚本。您页面上的第一个脚本

<script type="text/javascript" src="/test/wp-content/themes/child/script/jquery.jcarousel.min.js"></script>

似乎是一个 jQuery插件,由于 jQuery 尚未加载到页面上,因此可能会产生错误。

当站点的证书过期时,我也遇到了这个问题。今天我得到了它,因此我的所有网站都开始刷新失败。在您的控制台上,您只会看到:ReferenceError: jQuery is not defined 问题是直到您查看 URL 并看到以下内容后,您才能找到原因:code.jquery.com uses an invalid security certificate. The certificate expired on 07/31/2014 11:49 AM. The current time is 07/31/2014 12:40 PM. (Error code: sec_error_expired_certificate)
2021-04-20 12:08:10
也解决了我在 rails 中的问题。我首先在 jquery 之前包含了引导程序。但随后的解决方案是首先包含 Jquery,然后是引导程序。
2021-04-22 12:08:10
@aespinoza 解决方案有所帮助。澄清一下,在查看主页(站点根目录)之前,您不会看到错误。
2021-04-25 12:08:10
那太有用了!我在整个页面加载后包含它......
2021-05-01 12:08:10
什么是 jQuery 是页面上的第一个脚本,但还是有错误?
2021-05-16 12:08:10

你需要把它放在 wp_head() 之后;因为这会加载您的 jQuery,您需要先加载 jQuery,然后再加载您的 js

此答案仅适用于 Wordpress 网站
2021-04-18 12:08:10
这需要标记为答案。jQuery 首先加载,然后包含@JorgeMadafaka 提到的所有 js
2021-05-07 12:08:10
非常有帮助,非常感谢:)
2021-05-09 12:08:10

就我而言,发生错误是因为我使用了错误版本的 jquery。

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

我把它改成:

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

设置这个 jquery min js

script src="http://code.jquery.com/jquery-1.10.1.min.js"

在 wp-admin/admin-header.php

我认为这个答案很合适。但我不确定asker 是否使用wordpress。但它通过将 jq 添加到标题来解决问题。
2021-04-19 12:08:10
在主题之外更改 Wordpress 中的任何内容是个坏主意。实际上,您不需要为管理面板包含 jQuery,因为它已经包含在内了 :)
2021-04-20 12:08:10
@ Bernd Ott 我在 wordpress 中也遇到了同样的问题,我通过添加 min js 解决了这个问题,这就是我评论这篇文章的原因。
2021-05-07 12:08:10

首先,您似乎没有在标题中包含 jQuery 本身,而只是包含了一堆插件。至于 '<' 错误,不看生成的 HTML 就无法判断。