2017-06-16 10 views
0

私が開発しているwordpressプラグインは、wp_enqueue_scripts関数の3番目のパラメータに渡してもjquery依存関係を取得できません。 google chromeのinspect関数を使用すると、 "$は関数ではありません"というエラーが表示されるので、jqueryが正しく読み込まれていないと仮定しています。 jsファイルには 'here'と表示されています。どんな助けでも大歓迎です!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が関数を介して手作業で渡す必要がある$によってjQueryが定義されないことがあります。したがって、このような何かそれを修正する必要がありますワードプレスでパッケージ化

window.alert("here"); 

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

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

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

     }) 
    }); 

はまた、onclickのjQueryの方法ではありません、私は(あなたが.onを探していたと思います)

1

jqueryのは、他のJSにするために、互換モードが付属していますエイリアスとして$を使用すると一緒に実行できます。 $ egでコードを追加するためにjQueryラッパーを使用することができます。

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

か、あなたのJSファイルの先頭にvar $ = jQuery.noConflict();を追加することができます。

関連する問題