我有一個運行Master Slider Pro wordpress插件和iLightBox插件的網站。我在WordPress的兒童主題運行在custom.js文件的自定義腳本:jQuery有時按正確的順序加載,有時它不會
/* Link JavaScript file */
function custom_scripts() {
wp_register_script('custom-script', get_stylesheet_directory_uri() . '/assets/js/custom.js', array('jquery') , false, true);
wp_enqueue_script('custom-script');
}
我調用自定義文件按照下面我functions.php文件:
jQuery(document).ready(function(){
(function(){
var groupsArr = [];
jQuery('[rel^="ilightbox["]').each(function() {
var group = this.getAttribute("rel");
jQuery.inArray(group, groupsArr) === -1 && groupsArr.push(group);
});
jQuery.each(groupsArr, function (i, groupName) {
jQuery('[rel="' + groupName + '"]').iLightBox({ /* options */ });
});
})();
});
問題我有,有時它加載自定義腳本(我假設在正確的順序),我的滑塊加載和燈箱顯示當我單擊圖像...其他時間滑塊卡在加載狀態,並顯示錯誤:
Uncaught TypeError: jQuery(...).iLightBox is not a function
我相信我加載jQuery文件加載後的custom.js文件...我所假設的是,lighbox插件有時加載後的jQuery,因此加載後,custom.js文件,這就是爲什麼它不能找到ilightbox功能?
無論如何,我可以防止這個?
UPDATE: 這個傢伙不斷掙扎。我一直在做一些閱讀並與我的主題作者交談(他整合了ilightbox功能)。
我的代碼現在看起來像下面:
/* register script that defines ilightbox (in parent theme) */
wp_register_script('ilightbox_script', get_stylesheet_directory . '/includes/class-avada-scripts.php');
/* Link JavaScript file */
function custom_scripts() {
wp_register_script('custom-script', get_stylesheet_directory_uri() . '/assets/js/custom.js', array('jquery', 'ilightbox_script') , false, true);
wp_enqueue_script('custom-script');
}
add_action('wp_enqueue_scripts', 'custom_scripts', 99);
我相信現在我已經註冊了包含ilightbox功能(存儲在我父主題)的腳本。然後我在jQuery和註冊的腳本加載後調用custom.js文件。但它仍然無法正常工作。我也從文件中獲取我現在想註冊下列錯誤:
Uncaught SyntaxError: Unexpected token <
也許我誤會了一切,所以我抄什麼主題筆者對我說,當我問其中精定義ilightbox功能.. 你怎麼看?
這些腳本在以下目錄
/可溼性粉劑內容/主題/阿瓦達索/包括
和所有的JS代碼類阿瓦達索-scripts.php在 可排隊是main.min在下面的目錄
可用的.js/可溼性粉劑內容/主題/阿瓦達索/資產/ JS
但是如果從
0123使開發者模式Wp的管理 - >外觀 - >主題選項 - >高級
,那麼你可以修改阿瓦達索-lightbox.js代碼和lightbox.js 在同一目錄中。
你是如何入隊的燈箱插件? 'iLightBox'不包含在覈心jQuery中......所以你需要包含其他相關的腳本。如果您將它作爲插件的一部分包含在內,請查找該腳本已註冊的名稱,並將其添加到* custom.js *依賴項的數組中(例如'array('jquery','ilightbox_whatever')') – rnevius
請看原始信息我的更新 – user3290060