2017-06-16 120 views
0

我正在開發的wordpress插件無法引入jquery依賴項,儘管將它傳遞給wp_enqueue_scripts函數的第三個參數。在谷歌瀏覽器上使用檢查功能揭示了這個錯誤:「$不是一個函數」,所以我假設jquery沒有被正確加載。顯示'here'時正在到達js文件。任何幫助將非常感激!wp_enqueue_script不加載依賴關係

PHP管理頁面:

function kent_sidebar_plugin_scripts(){ 
wp_enqueue_script('kent_sidebar_process', 
plugin_dir_url(__FILE__).'inc/process.js', array('jquery')); 
} 

function kent_sidebar_add_admin_page(){ 

add_menu_page('Kent Sidebar Options', 'Kent Sidebar', 'manage_options', 
'kent_sidebar', 'kent_sidebar_options'); 

} 

add_action('admin_menu', 'kent_sidebar_add_admin_page'); 
add_action('admin_enqueue_scripts', 'kent_sidebar_plugin_scripts'); 


function kent_sidebar_options(){ 

require_once("inc/kent-sidebar-admin.php"); 

} 

JS文件:

window.alert("here"); 

$(document).ready(function(){ 

$('#editSidebar').onclick(function(){ 

    var optionVal = $('#sidebarList').find(":selected").text(); 
    alert(optionVal); 

}) 

});

回答

1

如果你通過插件加載你的腳本,jQuery有時沒有通過$來定義,你必須通過函數手動傳遞它。因此,像這樣應該修復它:

window.alert("here"); 

    jQuery(document).ready(function($){ 

     $('#editSidebar').on("click",function(){ 

      var optionVal = $('#sidebarList').find(":selected").text(); 
      alert(optionVal); 

     }) 
    }); 

另外的onclick不是一個jQuery的方法,我想你要找的。對()

1

jQuery的包裝與WordPress自帶的兼容性模式,使任何其他JS使用$作爲別名可以一起運行。你可以使用jQuery包裝來添加代碼,例如$。

(function($) { // your code with $ })(jQuery);

或者你可以在你的JS文件的頂部添加var $ = jQuery.noConflict();