admin管理员组文章数量:1406937
I am using $ in my custom js file in a wordpress child theme. But it says $ is not defined. But jQuery works fine. What is the reason? Is it because $ is used in newer version of jQUery and WP uses old version 1.12.4. Kindly correct me.
Kind Regards
I am using $ in my custom js file in a wordpress child theme. But it says $ is not defined. But jQuery works fine. What is the reason? Is it because $ is used in newer version of jQUery and WP uses old version 1.12.4. Kindly correct me.
Kind Regards
Share Improve this question asked Oct 25, 2019 at 13:19 Atta Ur Rehman AkbarAtta Ur Rehman Akbar 214 bronze badges 1 |2 Answers
Reset to default 1WP uses noConflict(). If you want to use $ instead of jQuery you have a couple options.
Option #1 - use a self-invoking function:
(function($) {
$(document).ready(function(){
...
});
}(jQuery));
Option #2 - Set noConflict():
Add var $ = jQuery.noConflict();
at the top of your custom JS file.
As different JS Frameworks use the $ for it's functions (jQuery, MooTools), Wordpress loads jQuery in the "noConflict"-Mode, which means that it doesn't use the $ but only jQuery.
You can now
- use jQuery instead of $
- encapsule your calls like RiddleMeThis proposed
On a personal note: honestly, it's not that big a deal to replace the $ with jQuery, isn't it? ;)
Happy Coding!
本文标签: pluginsjQuery works instead of
版权声明:本文标题:plugins - jQuery works instead of $ 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745053772a2639811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
jQuery
works better than$
even when$
works – Tom J Nowell ♦ Commented Oct 25, 2019 at 13:42