我花了几个小时试图解决这个问题。
我正在尝试包含 shuffle.js在我的 WordPress 主题中。它依赖于 jQuery 1.9+ 和 Modernizr。
我已通过 wp_enqueue_script
添加了我的脚本(见下文)
wp_enqueue_script( 'jquery');
wp_enqueue_script( 'modernizer', get_stylesheet_directory_uri() . '/js/modernizr.custom.js', array( 'jquery' ), '1', true );
wp_enqueue_script( 'shuffle', get_stylesheet_directory_uri() . '/js/jquery.shuffle.js', array( 'modernizer' ), '1', true );
wp_enqueue_script( 'my-shuffle', get_stylesheet_directory_uri() . '/js/my-shuffle.js', array( 'shuffle' ), '1', true );
我使它们相互依赖,并将它们加载到页脚。
我已将 3 个 .js
文件包装在一些 jQuery noConflict 包装器中(见下文)
(function($) {
// Inside of this function, $() will work as an alias for jQuery()
// and other libraries also using $ will not be accessible under this shortcut
})(jQuery);
我仍然收到错误:
Uncaught TypeError: Cannot read property 'fn' of undefined
jquery.shuffle.js?ver=1:1212
Uncaught TypeError: Object [object Object] has no method 'shuffle'
my-shuffle.js?ver=1.6
我读到的所有内容都表明某些内容未按顺序加载,jQuery 未加载,但我认为情况并非如此。当然我可能是错的:)
有人有什么想法我可以尝试吗?
[更新]
我获取了 jQuery 1.11.1 的缩小版本,并将其替换为与 Wordpress 捆绑的版本,一切都开始发挥作用!
只损失了半天:(
[更新2]
请参阅下面凯文的回答。无需更改捆绑的 jQuery,shuffle.js 有更新版本
最佳答案
Shuffle.js 错误地使用了 window.$
而不是 window.jQuery
。通过替换 this line 来修复它:
factory(window.$, window.Modernizr);
这个:
factory(window.jQuery, window.Modernizr);
这是 near the top .
<小时/>作者已经修复了这个问题,您可以从这里下载修复版本:https://github.com/Vestride/Shuffle/blob/e6cb28bd1eb4a5ff60b5d2925b1e26d857f3ec2a/dist/jquery.shuffle.js
关于javascript - 类型错误 : 'undefined' is not an object (evaluating '$.fn' ) - Wordpress & shuffle. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23813072/