2017-06-16 80 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();