admin管理员组文章数量:1287972
I am using this as my starter template: www.html5reset/
It's pretty nice, but I believe something is wrong with jQuery in there.
In the functions.php
it says:
// Load jQuery
if ( !function_exists(core_mods) ) {
function core_mods() {
if ( !is_admin() ) {
wp_deregister_script('jquery');
wp_register_script('jquery', ("//ajax.googleapis/ajax/libs/jquery/1.6.2/jquery.min.js"), false);
wp_enqueue_script('jquery');
}
}
core_mods();
}
This seems fine to me - is it alright?
There also is a premade JS file. In this file it says:
// remap jQuery to $
(function($){})(window.jQuery);
I don't know really what this does, but it seems also ok.
To test if jQuery works, I tried the following - none of it is woking, jQuery is not defined.
$(document).ready(function() {
alert("This is a test.");
});
jQuery(document).ready(function() {
alert("This is a test.");
});
$(document).ready(function($) {
alert("This is a test.");
});
So, can you tell ma what could be wrong with this? Thank you!
I am using this as my starter template: www.html5reset/
It's pretty nice, but I believe something is wrong with jQuery in there.
In the functions.php
it says:
// Load jQuery
if ( !function_exists(core_mods) ) {
function core_mods() {
if ( !is_admin() ) {
wp_deregister_script('jquery');
wp_register_script('jquery', ("//ajax.googleapis/ajax/libs/jquery/1.6.2/jquery.min.js"), false);
wp_enqueue_script('jquery');
}
}
core_mods();
}
This seems fine to me - is it alright?
There also is a premade JS file. In this file it says:
// remap jQuery to $
(function($){})(window.jQuery);
I don't know really what this does, but it seems also ok.
To test if jQuery works, I tried the following - none of it is woking, jQuery is not defined.
$(document).ready(function() {
alert("This is a test.");
});
jQuery(document).ready(function() {
alert("This is a test.");
});
$(document).ready(function($) {
alert("This is a test.");
});
So, can you tell ma what could be wrong with this? Thank you!
Share Improve this question asked Jul 24, 2012 at 21:21 SvenSven 3711 gold badge6 silver badges18 bronze badges 1 |2 Answers
Reset to default 7just to help a bit further ... WordPress runs jQuery in 'safe' mode
which means in WordPress you need to write code like this
jQuery(document).ready(function() {
and not like this
$(document).ready(function() {
But what the HTML5BP has done is added this funky bit of code (probably kindly to help Developers)
// remap jQuery to $
(function($){})(window.jQuery);
FYI there is a 3rd method of switching back to the $ symbol for jQuery which is to start your code like this:
jQuery(document).ready(function($) {
from this point on in your code you can now use $ to refer to jQuery
There's a typo in wp_register_script()
where "http:" is missing in the URL. Fix that and you'll at least be loading jQuery.
As an aside, many consider it not a great practice to overwrite the default WordPress jQuery and I've removed the code you reference from my customized HTML5reset blank theme that I use.
本文标签: jQuery in WordpressWhy is it not working
版权声明:本文标题:jQuery in Wordpress - Why is it not working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329700a2372697.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
(function($){})(window.jQuery);
is a wrapped method (to avoid polluting the global namespace), so I do not see how$
would be available outside that function. – Joseph Leedy Commented Jul 25, 2012 at 2:39