未捕获的 ReferenceError:未定义 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17419121/
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
Uncaught ReferenceError: jQuery is not defined
提问by user2541351
I'm trying to put together a Wordpress site with a JQuery slider plugin, but the plugin doesn't display on the page and I always get the above error message.
我正在尝试将 Wordpress 站点与 JQuery 滑块插件放在一起,但该插件未显示在页面上,而且我总是收到上述错误消息。
Despite trying several different fixes suggested in other's posts I still can't seem to fix the above error, including putting the script tag in the 'header.php' file. Any help would be much appreciated - thanks!
尽管尝试了其他帖子中建议的几种不同修复方法,但我似乎仍然无法修复上述错误,包括将脚本标记放在“header.php”文件中。任何帮助将不胜感激 - 谢谢!
Relevant code in 'footer.php' file
'footer.php' 文件中的相关代码
<!--Load JQuery-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
</body>
</html>
The Website:http://www.advanceprojects.com.au/
网站:http://www.advanceprojects.com.au/
回答by Sushanth --
Scripts always run in a sequential order.
脚本始终按顺序运行。
So basically you are trying to use jQuery even before the jQuery library is loaded.
所以基本上你甚至在 jQuery 库加载之前就尝试使用 jQuery。
Your script is dependent on Nivo which in turn depends on jQuery library.
您的脚本依赖于 Nivo,而 Nivo 又依赖于 jQuery 库。
Either move the script to the line after the library is loaded, or move the library declaration to the head.
在加载库后将脚本移至该行,或将库声明移至头部。
Also make sure you enclose the script inside DOM Ready
handler.
还要确保将脚本包含在DOM Ready
处理程序中。
So the order in which you should be loading these are
所以你应该加载这些的顺序是
-- jQuery
-- Nivo Slider
-- your script that uses the above 2 libraries.
回答by Phil Hudson
In a recent version of Wordpress this can be caused by core performance improvements:
在最新版本的 Wordpress 中,这可能是由核心性能改进引起的:
Try placing this in your wp.config file:
尝试将其放在 wp.config 文件中:
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
define('CONCATENATE_SCRIPTS', false);
回答by Dismi Paul
This worked for me.
这对我有用。
Add the line
添加行
wp_enqueue_script('jquery');
This error happened because jquery is not included, and should be included before loading any other script. Here is an example:
发生此错误是因为未包含 jquery,应在加载任何其他脚本之前包含 jquery。下面是一个例子:
firstplugin.php
第一个插件.php
<?php
/*
Plugin Name: Plugintrial
Description: plugin to test jquery
Version: 1
Author: Dismi Paul
*/
function childtheme_custom_login() {
wp_enqueue_script('jquery');
wp_enqueue_script('my-custom-script', plugins_url('/test.js', __FILE__));
?>
<h1>Welcome to My Homepage</h1>
<p id="intro">My name is Donald.</p>
<p>I live in Duckburg.<?php echo plugins_url('/test.js', __FILE__); ?></p>
<?php
}
add_action('login_head', 'childtheme_custom_login');
test.js
测试.js
jQuery(document).ready(function()
{
jQuery("#intro").css("background-color", "yellow");
});
console.log("it works");
回答by Balachandran
See The Link How to add css and Js File
请参阅链接如何添加 css 和 Js 文件
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Probably Before JQuery,Your Js File Is Loaded.
可能在 JQuery 之前,您的 Js 文件已加载。
So add js file after Jquery file is Loaded
所以在Jquery文件加载后添加js文件
Example
例子
wp_enqueue_script("yourjs","yourjspath",array('jquery'), true);
回答by som
Use
用
jQuery(document).ready(function(){
jQuery('#wpns_slider').nivoSlider({effect:'random',slices:2,});
});
instead of
代替
jQuery(window).load(function(){
jQuery('#wpns_slider').nivoSlider({effect:'random',slices:2,});
});
$(window).load(function() {... })
is explicitly binding to the window's load event, your code has already fired before jQuery
library is loaded. You have to use DOM Ready
handler.
$(window).load(function() {... })
显式绑定到窗口的加载事件,您的代码在jQuery
加载库之前已经触发。你必须使用DOM Ready
处理程序。