![](/img/trans.png)
[英]Trying to use Shuffle.js but getting Uncaught TypeError: Shuffle is not a constructor
[英]TypeError: 'undefined' is not an object (evaluating '$.fn') - Wordpress & shuffle.js
我已經花了數小時試圖解決這個問題。
我正在嘗試在我的Wordpress主題中包含shuffle.js 。 它取決於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 Wrappers中(請參見下文)
(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捆綁在一起的版本替換了它,然后全部付諸行動!
僅損失了半天時間:(
請參閱下面的凱文答案。 無需更改捆綁的jQuery,有shuffle.js的更新版本
Shuffle.js錯誤地使用window.$
而不是window.jQuery
。 通過替換此行來修復它:
factory(window.$, window.Modernizr);
與此:
factory(window.jQuery, window.Modernizr);
它在頂部附近 。
作者已經解決了這個問題,您可以從此處下載修復的版本: https : //github.com/Vestride/Shuffle/blob/e6cb28bd1eb4a5ff60b5d2925b1e26d857f3ec2a/dist/jquery.shuffle.js
除了告訴您實際上並未定義jQuery
,而且您必須在瀏覽器中進行調試之外,我們無法為您提供幫助。
檢查頁面的來源,找到您的<script src="jquery...">
並確定該文件是否為
jQuery
和以上 <script>
標記之上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.