问题描述
成为WP开发人员已有8年了,这个问题从未消失,这让我发疯. $ is not a function
总是出现,或者jQuery is not a function
或foundation is not a function
...有人帮我一劳永逸地将其压在萌芽中吗?
Been a WP dev for 8 years, this problem has never gone away and it's driving me insane. $ is not a function
always show up, or jQuery is not a function
, or foundation is not a function
... someone help me nip this in the bud once and for all?
我的enqueue.php:
My enqueue.php:
// initialize packages ...
function source_enqueue() {
// init $wp_styles variable
// adds conditional wrapper around ie stylesheet
global $wp_styles;
if (!is_admin()) {
// jquery, bower, foundation, app js, style, app css
// init jquery ...
wp_enqueue_script( 'jquery' );
// init foundation
wp_enqueue_script( 'foundation', 'https://cdn.jsdelivr.net/g/[email protected](js/foundation.min.js+js/vendor/fastclick.js+js/vendor/jquery.cookie.js+js/vendor/jquery.js+js/vendor/modernizr.js+js/vendor/placeholder.js),[email protected]', array(), '2.1.3', true );
// init normalize
wp_enqueue_style( 'foundation', 'https://cdn.jsdelivr.net/g/[email protected](css/normalize.css),[email protected],[email protected]', array(), '', 'all' );
// init app.min.js
wp_enqueue_script( 'app', get_template_directory_uri() . '/assets/dist/app.min.js', array( 'jquery' ), '', true );
// init app.min.css
wp_enqueue_style( 'app', get_template_directory_uri() . '/assets/dist/app.min.css', array(), '', 'all' );
// init style.css
wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css', array(), '', 'all' );
// init comment reply ...
if ( is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
wp_enqueue_script( 'comment-reply' );
}
}
}
// init source_enqueue() ...
// - - - - - - - - - - - - - - - - -
add_action( 'init', 'source_enqueue' , 999 );
我的app.js:
$.noConflict();
$(document).foundation();
我尝试将其包装在不同的功能中都无济于事:
I have tried wrapping it in different functions to no avail:
jQuery(document).ready(function(){
jQuery(document).foundation();
});
或:
$(document).ready(function(){
$(document).foundation();
});
没有任何效果.错误仍然存在:$ is not a function
.
Nothing works. The error still remains: $ is not a function
.
有人会帮助我彻底解决这个问题,然后再发疯并将漂亮的桌面扔进刨木机吗?
Someone help me solve this once and for all before I go mad and throw my nice desktop into a woodchipper?
推荐答案
经过多年的挖掘,测试和试验,我找到了答案.我决定最后一枪,它奏效了.没有错误,并且控制台注册了基础.对于所有苦苦挣扎的人来说,这是可以使您入队的基础脚本正常工作的代码.我什至会提供CDN.
I found the answer, after years of digging, testing, and trials. I decided to give this one last shot and it worked. No errors, and the console registers foundation. To all those who have struggled, this is the code that will let your enqueue'd foundation script work properly. I'll even provide the CDN.
需要2天才能接受我自己的答案.
enqueue.php/functions.php,但是您需要使用函数:
enqueue.php / functions.php, however you require your functions:
// enqueue.php or functions.php
wp_enqueue_script( 'foundation', 'https://cdn.jsdelivr.net/g/[email protected](js/foundation.min.js+js/vendor/jquery.js+js/vendor/modernizr.js),[email protected]', array(), '', true );
app.js:
// app.js
$ = jQuery.noConflict( true );
(function( $ ) {
$(document).foundation();
// other code here ...
})(jQuery);
这篇关于WordPress排队noConflict()错误$不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!